简体   繁体   English

如何在 SQL 服务器的 CASE 语句中加入 unicode 字符?

[英]How to incorporate unicode character in a CASE statement in SQL Server?

I have a column of type 'nvarchar' where some of the values have special characters.我有一列类型为“nvarchar”的列,其中一些值具有特殊字符。 What I want to do is, if a value starts with a special character, then perform a function to remove that character, else return the value as is.我想要做的是,如果值以特殊字符开头,则执行 function 删除该字符,否则按原样返回值。 The sample data is as follows:样本数据如下:

ID ID Amount数量
1 1个 999.09999.09 999.09999.09
2 2个 339.58339.58 339.58339.58
3 3个 2141.12055.72357.6 2141.12055.72357.6
4 4个 519.32519.32 519.32519.32
5 5个 661.84661.84 661.84661.84
6 6个 843.59843.59 843.59843.59

I tried to use the 'STUFF' function to replace a special character whenever a value starts with it.我尝试使用“STUFF”function 来替换值以它开头的特殊字符。 ie, IE,

SELECT  ID,
        STUFF (Amount, 1, 1, '') AS Test,
FROM        table

I works for individual value.我为个人价值而工作。

But when I apply this to the whole column using CASE statement (since all values don't start with a special character), then I fail to incorporate the special character in LIKE operator.但是当我使用 CASE 语句将它应用于整个列时(因为所有值都不以特殊字符开头),那么我无法将特殊字符合并到 LIKE 运算符中。 I tried the following query:我尝试了以下查询:

SELECT  ID,
        CASE
            WHEN Amount LIKE N'%'
            THEN (STUFF (Amount, 1, 1, ''))
            ELSE Amount
        END AS Test,
        
FROM        table

Results Expected:预期结果:

ID ID Amount数量 Test测试
1 1个 999.09999.09 999.09999.09 999.09999.09 999.09999.09
2 2个 339.58339.58 339.58339.58 339.58339.58 339.58339.58
3 3个 2141.12055.72357.6 2141.12055.72357.6 2141.12055.72357.6 2141.12055.72357.6
4 4个 519.32519.32 519.32519.32 519.32519.32 519.32519.32
5 5个 661.84661.84 661.84661.84 661.84661.84 661.84661.84
6 6个 843.59843.59 843.59843.59 843.59843.59 843.59843.59

The data looks like:数据看起来像:

在此处输入图像描述

As your data is either numerics or special characters you could just check for it not being a number:由于您的数据是数字或特殊字符,您可以检查它是否不是数字:

case when Amount not like N'[0-9]%' then (Stuff (Amount, 1, 1, '')) else Amount end as Test

Loosing the other characters can be done like this:松开其他字符可以这样做:

WITH nrs as (
    SELECT 1 as x 
    union all
    select x+1 from nrs where x<25
  ),
cte as 
        (select Amount,substring(Amount,1,1) as a from mytable
         union all
         select Amount,substring(Amount,x+1,1) as a
         from mytable
         cross apply nrs x)
select mytable.Amount, string_agg(a,'') as NewNumber
from mytable
inner join cte on cte.Amount = mytable.Amount and a<>''
where a between '0'and '9' or a='.' or a=','
group by mytable.Amount;

output: output:

Amount数量 NewNumber新号码
?339.58?339.58 ?339.58?339.58 339.58339.58 339.58339.58
?519.32?519.32 ?519.32?519.32 29.312539.15 29.312539.15
?661.84?661.84 ?661.84?661.84 66.186468.14 66.186468.14
?843.59?843.59 ?843.59?843.59 93.549853.48 93.549853.48
?999.09?999.09 ?999.09?999.09 9990.99990.9 9990.99990.9
2141.1?2055.72?357.6 2141.1?2055.72?357.6 27357.6.55021.1421 27357.6.55021.1421

see: DBFIDDLE参见: DBFIDDLE

NOTE: NewNumber is still not numeric.注意: NewNumber仍然不是数字。 It just contains numbers and/or a ',' and/or a '.'.它仅包含数字和/或“,”和/或“.”。

You can use TRIM - for SQL Server 2019 and below, TRIM removes the leading and trailing characters.您可以使用 TRIM - 对于 SQL Server 2019 及以下版本,TRIM 会删除前导字符和尾随字符。 For SQL Server 2022 you can specify leading/trailing/both:对于 SQL Server 2022,您可以指定前导/尾随/两者:

Sample Data:样本数据:

Declare @testData Table (ID int, Amount nvarchar(50));
 Insert Into @testData (ID, Amount)
 Values (1, N'999.09999.09')
      , (2, N'339.58339.58')
      , (3, N'2141.12055.72357.6')
      , (4, N'519.32519.32')
      , (5, N'661.84661.84')
      , (6, N'843.59843.59');

SQL Server 2017: SQL 服务器 2017:

 Select *
      , Test = trim(N'' From td.Amount)
   From @testData td;

SQL Server 2022: SQL 服务器 2022:

 Select *
      , Test = trim(leading N'' From td.Amount)
   From @testData td;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM