我需要對存儲URL編碼文本的遺留表運行查詢。我需要在我的結果中解讀這段文字。我如何實現這一目標?SQL Server網址解碼
10
A
回答
20
嘗試其中之一:
CREATE FUNCTION dbo.UrlDecode(@url varchar(3072))
RETURNS varchar(3072)
AS
BEGIN
DECLARE @count int, @c char(1), @cenc char(2), @i int, @urlReturn varchar(3072)
SET @count = Len(@url)
SET @i = 1
SET @urlReturn = ''
WHILE (@i <= @count)
BEGIN
SET @c = substring(@url, @i, 1)
IF @c LIKE '[!%]' ESCAPE '!'
BEGIN
SET @cenc = substring(@url, @i + 1, 2)
SET @c = CHAR(CASE WHEN SUBSTRING(@cenc, 1, 1) LIKE '[0-9]'
THEN CAST(SUBSTRING(@cenc, 1, 1) as int)
ELSE CAST(ASCII(UPPER(SUBSTRING(@cenc, 1, 1)))-55 as int)
END * 16 +
CASE WHEN SUBSTRING(@cenc, 2, 1) LIKE '[0-9]'
THEN CAST(SUBSTRING(@cenc, 2, 1) as int)
ELSE CAST(ASCII(UPPER(SUBSTRING(@cenc, 2, 1)))-55 as int)
END)
SET @urlReturn = @urlReturn + @c
SET @i = @i + 2
END
ELSE
BEGIN
SET @urlReturn = @urlReturn + @c
END
SET @i = @i +1
END
RETURN @urlReturn
END
GO
從http://sqlblog.com/blogs/peter_debetta/archive/2007/03/09/t-sql-urldecode.aspx
CREATE FUNCTION dbo.fnDeURL
(
@URL VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Position INT,
@Base CHAR(16),
@High TINYINT,
@Low TINYINT,
@Pattern CHAR(21)
SELECT @Base = 'abcdef',
@Pattern = '%[%][0-9a-f][0-9a-f]%',
@URL = REPLACE(@URL, '+', ' '),
@Position = PATINDEX(@Pattern, @URL)
WHILE @Position > 0
SELECT @High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base COLLATE Latin1_General_CI_AS),
@Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base COLLATE Latin1_General_CI_AS),
@URL = STUFF(@URL, @Position, 3, CHAR(16 * @High + @Low - 17)),
@Position = PATINDEX(@Pattern, @URL)
RETURN @URL
END
+0
與第一個鏈接。你我的朋友,是一個拯救生命的人。謝謝! – 2010-09-30 17:42:15
0
的代碼不適合我.... 這裏我的代碼:
create function [UrlDecode](@url nvarchar(max))
returns nvarchar(max)
as
begin
declare @output nvarchar(max), @C nvarchar(255), @C1 nvarchar(255), @C2 nvarchar(255), @IsUrl int
set @output = ''
declare @T table(I int identity(1,1), C nvarchar(255))
insert @T(C)
select items from dbo.Split(@url,'%')
set @IsUrl = charindex('%', @url)
declare cur cursor for select C from @T order by I
open cur
fetch next from cur into @C
while @@fetch_status = 0 begin
set @C1 = substring(@C, 2, 4)
set @C2 = substring(@C, 6, 255)
if substring(@C, 1, 1) = 'u' and @IsUrl <> 0 begin
if @C1 is not null
set @output = @output + char((CONVERT(INT,CONVERT(VARBINARY(4),@C1,2)))-1264)
if @C2 is not null
set @output = @output + @C2
end else begin
set @output = @output + isnull(substring(@C, 1, 255),'')
end
fetch next from cur into @C
end
close cur
deallocate cur
return @output
end`
「分割」功能:
create function [Split](@String nvarchar(max), @Delimiter char(1))
returns @temptable table (items nvarchar(max))
as
begin
set @String = ltrim(rtrim(@String))
declare @idx int
declare @slice nvarchar(max)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
0
create function [dbo].[URLDecode](
@str nvarchar(max)
) returns nvarchar(max)
begin
if @str is null return null
declare @out nvarchar(max) = N''
set @str = replace(@str, N'+', N'%20')
while len(@str) > 0
begin
declare @i bigint = patindex(N'%[%][0-9a-fA-F][0-9a-fA-F]%', @str)
if @i = 0 break
set @out = @out + substring(@str, 0, @i) + convert(nchar(2), convert(varbinary, '0x' + substring(@str, @i + 1, 2), 1))
set @str = substring(@str, @i + 3, len(@str))
end
return @out + @str
end
相關問題
- 1. 與解碼網址
- 2. 解碼/編碼在SQL Server
- 3. 解碼Json的網址
- 4. ios - NSString網址解碼?
- 5. 解釋SQL Server代碼
- 6. 如何在SQL Server拆分網址
- 7. 試圖解碼國際化的網址
- 8. 網址不會自動解碼$ _GET
- 9. C++網址解碼,utf8字符錯誤
- 10. HTML解碼SQL Server中的字符串
- 11. 'ascii'編解碼器無法解碼 - 通過pyodbc CSV到SQL Server
- 12. JavaScript網址解析
- 13. 解析JSON網址
- 14. SQL Server密碼
- 15. SQL地址解析
- 16. SQL:檢查網址
- 17. 解釋SQL Server鎖
- 18. Dropbox網址掩碼
- 19. htaccess網址掩碼
- 20. Drupal網址編碼
- 21. 級聯碼網址
- 22. Twitter網址編碼
- 23. 如何解碼谷歌瀏覽器中的編碼網址?
- 24. 通過平臺API編碼/解碼SEO網址?
- 25. 編碼網址使用JavaScript,解碼與PHP
- 26. 如何最好地理解SQL Server中的地址表?
- 27. ASP從SQL Server數據庫的動態網址
- 28. 通過網址傳遞參數到sql server報告服務
- 29. 將從網址獲取的圖像插入到SQL Server中
- 30. 將Oracle解碼語句轉換爲SQL Server T-SQL語句
如果您正在使用的編程語言來運行查詢,它可能是最簡單的URL編碼搜索查詢也是如此。 – 2010-09-30 17:33:37