【问题标题】:In SQL Server, replace a Char(0), the null character, embedded in a string with its hex code在 SQL Server 中,将嵌入在字符串中的空字符 Char(0) 替换为其十六进制代码
【发布时间】:2012-06-12 17:08:13
【问题描述】:

什么是 SQL Server T-SQL 中的构造,它将用其十六进制代码替换嵌入在字符串中的 Char(0)(空字符)?

 REPLACE('t'+Char(0)+'t', Char(0), REPLACE(master.dbo.fn_varbintohexstr(0), '000000', ''))

不返回't0x00t',是什么? (这已经适用于 1 到 31。)

这个question 有解释问题是排序规则的答案。

answer 显示master.dbo.fn_varbintohexstr(0) 将返回0x00000000

当我手动将内部 Replace 简化为 '0x00' 时,只需添加一个 Collate 子句就可以让它工作,就像上面建议的问题的答案一样,但我找不到适用的版本完整的表达式(然后可用于所有 n,0 到 31,跳过 9、10 和 13)。

【问题讨论】:

    标签: sql-server replace hex nul


    【解决方案1】:

    你想要的可能是这个。

    SELECT REPLACE('t'+Char(0)+'t', Char(0), CONVERT(VARCHAR(10),REPLACE(master.dbo.fn_varbintohexstr(0), '000000', '')))
    

    您需要将其显式转换为 VARCHAR

    如果你想要完整的表达式删除内部替换

    SELECT REPLACE('t'+Char(0)+'t', Char(0), CONVERT(VARCHAR(10),master.dbo.fn_varbintohexstr(0)))
    

    【讨论】:

    • 为了完整起见最后的 SQL:with m as (select 0 as n union all select n+1 from m where n<31) update Messages set MessageText = replace(MessageText, char(n) collate SQL_Latin1_General_CP1_CS_AS, CONVERT(VARCHAR(10), replace(master.dbo.fn_varbintohexstr(5), '000000', ''))) from m where n not in (9, 10, 12, 13) and MessageText like '%' + char(n) collate SQL_Latin1_General_CP1_CS_AS + '%'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2022-07-22
    • 2020-07-18
    相关资源
    最近更新 更多