如果我的数据中|
之前的字符数始终不同,我怎么能写一个SELECT
语句来将所有字符都添加到管道符?
示例数据:
asdf|adkfdll|dd
asdkdkdk|da|d
答案 0 :(得分:5)
;WITH T(C) AS
(
SELECT 'asdf|adkfdll|dd' UNION ALL
SELECT 'asdkdkdk|da|d' UNION ALL
SELECT ''
)
SELECT LEFT(C, CHARINDEX('|',C + '|') -1)
FROM T
答案 1 :(得分:3)
您可以将charindex
与substring
:
select substring(col1, 1, charindex('|',col1))
from (
select 'asdf|adkfdll|dd' as col1
union all
select 'asdkdkdk|da|d'
) as YourData
where charindex('|',col1) > 0