如何在 SQL Server 中拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6619810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:15:54 来源:igfitidea点击:
How to split strings in SQL Server
提问by himadri
I have the following input:
我有以下输入:
Data
-----
A,10
A,20
A,30
B,23
B,45
Expected output:
预期输出:
col1 Col2
---- -----
A 10
A 20
A 30
B 23
B 45
How can I split the string to produce the desired output?
如何拆分字符串以产生所需的输出?
回答by niktrs
SELECT substring(data, 1, CHARINDEX(',',data)-1) col1,
substring(data, CHARINDEX(',',data)+1, LEN(data)) col2
FROM table
回答by t-clausen.dk
I know the points has already been given, going to post it anyway because i think it is slightly better
我知道积分已经给了,反正我会贴出来,因为我觉得它稍微好一点
DECLARE @t TABLE (DATA VARCHAR(20))
INSERT @t VALUES ('A,10');INSERT @t VALUES ('AB,101');INSERT @t VALUES ('ABC,1011')
SELECT LEFT(DATA, CHARINDEX(',',data) - 1) col1,
RIGHT(DATA, LEN(DATA) - CHARINDEX(',', data)) col2
FROM @t
回答by rahularyansharma
declare @string nvarchar(50)
set @string='AA,12'
select substring(@string,1,(charindex(',',@string)-1) ) as col1
, substring(@string,(charindex(',',@string)+1),len(@string) ) as col2![my sql server image which i tried.][1]
回答by vlad
回答by Mehdi najafian
it is so easy, you can take it by below query:
它是如此简单,您可以通过以下查询获取它:
SELECT LEFT(DATA, CHARINDEX(',',DATA)-1) col1,RIGHT(Data,LEN(DATA)-(CHARINDEX(',',DATA))) col2 from Table