April 13, 2015

Tricky question from UPDATE statement in SQL Server

We have one varchar field in SQL Server table containing numeric value with Null entries. Now if someone want to update this value how can we do this ?

DECLARE @Table TABLE
(
       VAL VARCHAR(100)
)

INSERT INTO @Table
SELECT  1000 UNION ALL
SELECT  Null UNION ALL
SELECT  5000

UPDATE @Table
SET
VAL = CAST(VAL AS INT) + 100

SELECT * FROM @Table

Output:

image

No comments:

Post a Comment