SQL Server : how to split a string using a comma as a separator [duplicate]

余生长醉 提交于 2019-12-06 11:45:43

问题


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.

What I'm hoping to be able to come up with is a simple UPDATE query as per the below:

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

Any ideas on how to achieve this?


回答1:


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


来源:https://stackoverflow.com/questions/14852747/sql-server-how-to-split-a-string-using-a-comma-as-a-separator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!