Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 转换UTF<;U+;某物>;对女巫_Sql_Sql Server - Fatal编程技术网

Sql 转换UTF<;U+;某物>;对女巫

Sql 转换UTF<;U+;某物>;对女巫,sql,sql-server,Sql,Sql Server,我有一个奇怪的UTF字符串存储: <U+0410><U+043B><U+044C><U+043A><U+0430> 通过R transform将数据从Power BI发送到SQL Server时,我得到了这种奇怪的格式,具体方式如下: Jeroen Mostert在评论中的回答似乎解决了这个问题。谢谢。要跨多个列值使用此函数,您需要将其转换为表值函数,并通过交叉应用调用它,尽管我相信您可以自己管理。评论中有解释: declare @

我有一个奇怪的UTF字符串存储:

<U+0410><U+043B><U+044C><U+043A><U+0430>
通过R transform将数据从Power BI发送到SQL Server时,我得到了这种奇怪的格式,具体方式如下:


Jeroen Mostert在评论中的回答似乎解决了这个问题。谢谢。

要跨多个列值使用此函数,您需要将其转换为表值函数,并通过
交叉应用
调用它,尽管我相信您可以自己管理。评论中有解释:

declare @str nvarchar(1000) = '<U+0410><U+043B><U+044C><U+043A><U+0430> This is a string with <U+0410><U+043B><U+044C><U+043A><U+0430> not encoded as we would like <U+0410><U+043B><U+044C><U+043A><U+0430>';

-- Add an additional > character before the first < character to act as the first delimiter
-- and then insert a delimiting > character before any instances of a < chracter that follow a space to ensure the character code is properly parsed out.
select @str = replace(stuff(@str,charindex('<',@str,1),0,'>'),' <',' ><');

                -- Start tally table with 10 rows.
with n(n)   as (select n from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n(n))
                -- Select the same number of rows as characters in @str as incremental row numbers.
                -- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
    ,t(t)   as (select top (select len(@str) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)
                -- Return the position of every value that follows the specified delimiter.
    ,s(s)   as (select 1 union all select t+1 from t where substring(@str,t,1) = '>')
                -- Return the start and length of every value, to use in the SUBSTRING function.
                -- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
    ,l(s,l) as (select s,isnull(nullif(charindex('>',@str,s),0)-s,4000) from s)
    ,r      as (select rn as ItemNumber
                    ,Item
                from(select row_number() over(order by s) as rn
                            ,substring(@str,s,l) as item
                    from l
                    ) a
                where Item <> ''
                )
select cast((select case when left(Item,3) = '<U+'  -- Where required, convert the Unicode number into a character using the NCHAR function
                        then nchar(convert(nvarchar(500),convert(int,(convert(varbinary(max),replace(Item,'<U+','0x0000'),1)))))
                        else Item
                        end
            from r
            order by ItemNumber
            for xml path('')
            ) as nvarchar(max)) as String;

你能举几个例子说明你存储的字符串以及你想把它们转换成什么吗?@iamdave我已经更新了我的问题。你确定这就是字符串中的字面意思吗(如,
LEFT(s,1)=“在T-SQL中很难以同样快的方式可靠地做到这一点。你可以通过XML进行转换(
选择CONVERT(NVARCHAR(MAX)、CONVERT(XML、REPLACE(REPLACE(“”、“;”)))
)但是,如果您的字符串包含与此方案无关的
字符,则此操作会大大失败,我不愿意将其作为一种通用解决方案来推广。不过,更可靠的替换方法需要T-SQL不具备的字符串匹配功能。@Jeroenmoster是的,它起到了作用。当然也有这样的字符,比如数字。看起来只有转换为
的异国宪章谢谢,我知道什么是TVF。非常有帮助。@PrzemyslawRemin如果这是您发布的问题的正确解决方案,为了其他SO用户的利益,请将其标记为正确答案
declare @str nvarchar(1000) = '<U+0410><U+043B><U+044C><U+043A><U+0430> This is a string with <U+0410><U+043B><U+044C><U+043A><U+0430> not encoded as we would like <U+0410><U+043B><U+044C><U+043A><U+0430>';

-- Add an additional > character before the first < character to act as the first delimiter
-- and then insert a delimiting > character before any instances of a < chracter that follow a space to ensure the character code is properly parsed out.
select @str = replace(stuff(@str,charindex('<',@str,1),0,'>'),' <',' ><');

                -- Start tally table with 10 rows.
with n(n)   as (select n from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n(n))
                -- Select the same number of rows as characters in @str as incremental row numbers.
                -- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
    ,t(t)   as (select top (select len(@str) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)
                -- Return the position of every value that follows the specified delimiter.
    ,s(s)   as (select 1 union all select t+1 from t where substring(@str,t,1) = '>')
                -- Return the start and length of every value, to use in the SUBSTRING function.
                -- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
    ,l(s,l) as (select s,isnull(nullif(charindex('>',@str,s),0)-s,4000) from s)
    ,r      as (select rn as ItemNumber
                    ,Item
                from(select row_number() over(order by s) as rn
                            ,substring(@str,s,l) as item
                    from l
                    ) a
                where Item <> ''
                )
select cast((select case when left(Item,3) = '<U+'  -- Where required, convert the Unicode number into a character using the NCHAR function
                        then nchar(convert(nvarchar(500),convert(int,(convert(varbinary(max),replace(Item,'<U+','0x0000'),1)))))
                        else Item
                        end
            from r
            order by ItemNumber
            for xml path('')
            ) as nvarchar(max)) as String;
+----------------------------------------------------------------------+
| String                                                               |
+----------------------------------------------------------------------+
| Алька This is a string with Алька not encoded as we would like Алька |
+----------------------------------------------------------------------+