3

I want to set column value only if it is not blank.

Here is sample

Declare @Temp Varchar(20)

Update Logins
Set ColValue = 
Case When @Temp <> '' Then @Temp Else /* Dont SET Value */ End
Where Code=1

What to write in Else Part ?

I have multiple columns to update and want to apply condition in only single column

DB : SQL SERVER 2008

Shaggy
  • 5,422
  • 28
  • 98
  • 163
  • 1
    http://stackoverflow.com/questions/14762374/how-do-i-use-case-in-sql-for-my-example may this help you – gasroot May 28 '13 at 08:56

2 Answers2

3

You have to set the updated column as below:

Declare @Temp Varchar(20)

Update Logins
Set ColValue = Case When @Temp <> '' Then @Temp 
                    Else ColValue 
               End
Where Code=1
Robert
  • 25,425
  • 8
  • 67
  • 81
0

You can check your variable in WHERE to not update at all when it's blank

Declare @Temp Varchar(20)

Update Logins
Set ColValue = @Temp
Where Code=1 AND @Temp <> ''
Nenad Zivkovic
  • 18,221
  • 6
  • 42
  • 55