简体   繁体   English

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

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

All, 所有,

Using SSMS v17.2 使用SSMS v17.2

Long story short I have 15 columns in a table, each record in the table has a different number of columns filled out, the rest are null. 长话短说,我在一个表中有15列,表中的每个记录都有不同数量的列填写,其余均为空。 I then concatenate the columns with a comma delimiter. 然后,我用逗号定界符将列连接起来。 Problem is the fields which are null also get concatenated leaving me with trailing commas. 问题是null字段也被连接在一起,并留下逗号结尾。

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

As you can see the very last character before the trailing commas can be either alpha numeric or special characters. 如您所见,尾随逗号前的最后一个字符可以是字母数字或特殊字符。 I need help removing these training commas up to the first character that is not a comma such as the below. 我需要帮助来删除这些训练逗号,直到第一个不是逗号的字符为止,如下所示。

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

Thank you 谢谢

This returns the correct data after concatenation: 串联后返回正确的数据:

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

This prevents the trailing commas from being concatenated: 这样可以防止尾随逗号串联在一起:

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, '')

Actually, the above only works if concatenating a null returns a null. 实际上,仅当串联null返回null时,以上方法才有效。 If this line: 如果这一行:

SELECT 'xxx' + NULL

returns 退货

xxx

then my second suggestion does not work 那我的第二个建议不起作用

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

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