23554

Question:
I have a table called TableReason
with a column called Reason
.
Reason
datatype is varchar(100)
and contains values like this 2,-2,22,33,0,2
for one row
I need to write an update statement for this table to accomplish: only need first value of split with comma, ie. 2 only needed.
update TableReason
set reason=--please help me on this.
Answer1:
...
set reason=CASE CHARINDEX(',', reason)
WHEN 0 THEN reason
WHEN 1 THEN ''
ELSE LEFT(reason, CHARINDEX(',', reason)-1)
END
This deals with the 3 cases of
<ul><li>no comma</li> <li>first character is a comma</li> <li>a number followed by a comma</li> </ul>