提问人:Pacoletaco 提问时间:11/2/2016 最后编辑:TheGameiswarPacoletaco 更新时间:2/21/2023 访问量:13644
Microsoft SQL 2016 解码 Base64 列
Microsoft SQL 2016 decode Base64 column
问:
我需要帮助来解码数据库中的 Base64 密码列。当我将单列内容复制到 https://www.base64decode.org/ 时,它可以完美地解码。我只需要对本列中的 ~7000 行执行此操作。任何帮助都非常感谢。
答:
1赞
dub stylee
11/2/2016
#1
如果尝试在 Management Studio(或直接使用 T-SQL)中执行此操作,则可以按如下方式完成:If you are trying to do this in Management Studio (or T-SQL directly ), then you can accomplish it like this:
declare @source varbinary(max), @encoded varchar(max), @decoded varbinary(max)
set @source = convert(varbinary(max), 'Hello Base64')
set @encoded = cast('' as xml).value('xs:base64Binary(sql:variable("@source"))', 'varchar(max)')
set @decoded = cast('' as xml).value('xs:base64Binary(sql:variable("@encoded"))', 'varbinary(max)')
select convert(varchar(max), @source) as source_varchar, @source as source_binary, @encoded as encoded, @decoded as decoded_binary, convert(varchar(max), @decoded) as decoded_varchar
3赞
Franco Ugarte
9/8/2017
#2
您可以使用以下(来源):
declare @source varbinary(max), @encoded varchar(max), @decoded varbinary(max)
set @source = convert(varbinary(max), 'Hello Base64')
set @encoded = cast('' as xml).value('xs:base64Binary(sql:variable("@source"))', 'varchar(max)') set @decoded = cast('' as xml).value('xs:base64Binary(sql:variable("@encoded"))', 'varbinary(max)')
select convert(varchar(max), @source) as source_varchar, @source as source_binary, @encoded as encoded, @decoded as decoded_binary, convert(varchar(max), @decoded) as decoded_varchar
...但是,为此创建一个函数,然后:
create function fnDecodeBase64 (@encoded as varchar(max))
returns varchar(max)
as
begin
declare @decoded varchar(max)
set @decoded = cast('' as xml).value('xs:base64Binary(sql:variable("@encoded"))', 'varbinary(max)')
return @decoded
end
因此,您可以使用:
select dbo.fnDecodeBase64(some_column) from Some_Table
您还可以使用:
select convert(varchar(max),cast(N'' as xml).value('xs:base64Binary(sql:column("t.SomeColumn"))', 'varbinary(max)')) as converted_64_column
from SomeTable as t
关键词:与众不同sql:column
评论