views:

33

answers:

1

I have a table with several columns that allow NULLs. How would I go about writing a SQL Query that will allow me to skip that column in an update if the value is "empty". Should I use a boolean flag letting the query know to update that value? I do something similar in a SELECT like this

SELECT * FROM table WHERE (@boolFlag = 1 OR col1 = @col1Val)

But trying to do that for an update has proven to be an exercise in futility. Ideally I'd be able to say skip this value if true, otherwise update over my 20ish columns. Right now I can't pass in DBNull.Value, so instead I'm being forced to insert "" which is converted into whitespace (for instance, my nvarchar(50) has 50 spaces).

I'm using a SQL Server DB/Table and a DataSet in VB.NET, constructing my queries in VS2k8 if that matters.

+1  A: 

You could do:

update MyTable
set MyColumn = case when @MyColumnValue = '' then null else @MyColumnValue end

A short form of the above would be:

update MyTable
set MyColumn = case when @MyColumnValue <> '' then @MyColumnValue end
RedFilter
I'll have to try this then. Thanks for the quick answer.
Crag
Ok this solution proved to be the right answer. The only thing I had to do after creating the SQL query was go and set parameters in VS2k8 (normally, prefacing them with the @ will create them for me, but for some reason they aren't found inside the case block).
Crag