TSCAmerica.com
TSCAmerica.com

Reputation: 5377

Update comma-separated column in SQL Server 2008

I have a table in SQL Server called Settings with a column settingValue which contains comma-separated values.

Sample data for that column:

0, 0
US, US
[email protected], [email protected]

I want to write an update query such that the column settingValue displays only one value that is it should result in

0
US
[email protected]

Not sure where to start shall I find comma (,) first and then truncate statement?

Upvotes: 0

Views: 1609

Answers (1)

Taryn
Taryn

Reputation: 247670

If you want to remove everything after the first comma, then you can use:

update yourtable
set col = substring(col, 1, charindex(',', col)-1)
where charindex(',', col) > 0  -- only update the rows with a comma

See SQL Fiddle with Demo

Upvotes: 4

Related Questions