[英]Replacing first occurrence of character with one character and the second with another character in 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.