SQL SERVER: Check if variable is null and then assign statement for Where Clause -


i trying achieve below in clause in sql.

if (@zipcode ==null) begin ([portal].[dbo].[address].position.filter(@radiusbuff) = 1)    end else if(@zipcode !=null) begin ([portal].[dbo].[address].postalcode=@zipcode ) end   

i tried following:

where ((@zipcode not null , ([portal].[dbo].[address].postalcode=@zipcode)) or (@zipcode null , ([portal].[dbo].[address].position.filter(@radiusbuff) = 1))) 

which wrong. can in framing exact statement. thanks!

isnull() syntax built in kind of thing.

declare @int int = null;  declare @values table ( id int, def varchar(8) )  insert @values values (8, 'i 8');  -- fails select * @values id = @int  -- works fine select * @values id = isnull(@int, 8); 

for example keep in mind can change scope yet predicate off of different variable complex boolean logic. caveat need cast differently if need examine different data type. if add row wish specify int of 8 , reference of text similar 'repeat' can reference again 'isnull' of first variable yet return entirely different result data type different reference different field.

declare @int int = null;  declare @values table ( id int, def varchar(16) )  insert @values values (8, 'i 8'), (8, 'i 8 repeat');  select * @values id = isnull(@int, 8) , def isnull(cast(@int varchar), '%repeat%') 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -