SQL Server:如何使用逗号作为分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14852747/
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
SQL Server : how to split a string using a comma as a separator
提问by Jediwannabe
I need to populate columns in my database for Latitude and Longitude, however the original information is stored as a single string
我需要在我的数据库中为纬度和经度填充列,但是原始信息存储为单个字符串
eg.
例如。
UDFChar1 = 41.243223,-8.183913
I am guessing that the TRIM command will come in useful here, but I do not know how I can tell it to stop exactly on the comma for each half.
我猜 TRIM 命令在这里会有用,但我不知道如何告诉它在每一半的逗号上完全停止。
What I'm hoping to be able to come up with is a simple UPDATE query as per the below:
我希望能够提出一个简单的 UPDATE 查询,如下所示:
UPDATE Asset
SET Lattitude = (SELECT LTRIM(UDFChar1)),
Longitude = (SELECT RTRIM(UDFChar1))
but obviously with some extra work in the LTRIM and RTRIM parts so that I am only selecting the data up to, and not including the comma in UDFChar1
但显然在 LTRIM 和 RTRIM 部分做了一些额外的工作,所以我只选择数据,而不包括 UDFChar1 中的逗号
Any ideas on how to achieve this?
关于如何实现这一目标的任何想法?
回答by TechDo
Please try:
请尝试:
left(Col, charindex(',', Col)-1)
and
和
right(Col, len(Col)-charindex(',', Col))
sample
样本
SELECT
LEFT(COL, CHARINDEX(',', Col)-1) Lattitude,
RIGHT(COL, LEN(COL)-CHARINDEX(',', Col)) Longitude
FROM(
SELECT '41.243223,-8.183913' Col
)x