繁体   English   中英

SQL:查找除指定字符外的任何字符的首次出现

[英]SQL: Find first occurrence of any character except the one specified

所有,

使用SSMS v17.2

长话短说,我在一个表中有15列,表中的每个记录都有不同数量的列填写,其余均为空。 然后,我用逗号定界符将列连接起来。 问题是null字段也被连接在一起,并留下逗号结尾。

ARNP,ACLS Provider,BLS Provider CPR,,,,,,,,,,,,
COC,CPC,CRC,CPC-1,,,,,,,,,,,
CISSP,CCNA Security,Network +,,,,,,,,,,,,
Leadership (All levels),Education (Grades K-12),,,,,,,,,,,,,,

如您所见,尾随逗号前的最后一个字符可以是字母数字或特殊字符。 我需要帮助来删除这些训练逗号,直到第一个不是逗号的字符为止,如下所示。

ARNP,ACLS Provider,BLS Provider CPR
COC,CPC,CRC,CPC-1
CISSP,CCNA Security,Network +
Leadership (All levels),Education (Grades K-12)

谢谢

串联后返回正确的数据:

SELECT LEFT(@MyData, charindex(',,', @Mydata) - 1)

这样可以防止尾随逗号串联在一起:

SELECT Col1 + ',' + IsNull(Col2 + ',', '') + IsNull(Col3 + ',', '') + IsNull(Col4 + ',', '') + 
    IsNull(Col5 + ',', '') + IsNull(Col6 + ',', '') + IsNull(Col7 + ',', '') + IsNull(Col8 + ',', '') +
    IsNull(Col9 + ',', '') + IsNull(Col10 + ',', '') + IsNull(Col11 + ',', '') + 
    IsNull(Col12 + ',', '') + IsNull(Col13 + ',', '') + IsNull(Col14 + ',', '') + IsNull(Col15, '')

实际上,仅当串联null返回null时,以上方法才有效。 如果这一行:

SELECT 'xxx' + NULL

退货

xxx

那我的第二个建议不起作用

暂无
暂无

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

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