Friday, February 24, 2012

Iif use

I am using this code, trying to set the bit value of @.temp1 using the Iif statement:

declare @.temp1 bit,
@.var1 varchar,
@.var2 varchar,
@.var3 varchar

select @.var1='testing', @.var2='testing2'

Select @.temp1 = Iif((@.var1 = @.var2), 1, 0)

select @.temp1

I get this error:
Line 9: Incorrect syntax near '='.

Any suggestions?
ThanksI belive yu are using IIF in the wrong context try:

case when @.var1 = @.var2 then 1 else 0 end|||declare @.temp1 bit,
@.var1 varchar,
@.var2 varchar,
@.var3 varchar

select @.var1='testing', @.var2='testing2', @.temp1=0

Select @.temp1 = case when @.var1 = @.var2 then 1 else 0 end

select @.temp1 as result

OK, I changed it to the codeabove. But now result comes back as 1, how is that possible since the 2 variables arent equal?|||try:

declare @.temp1 bit,
@.var1 varchar(10),
@.var2 varchar(10),
@.var3 varchar(10)
select @.var1='testing'
, @.var2='testing2'
, @.temp1=0
Select @.temp1 = case when @.var1 = @.var2 then 1 else 0 end
select @.temp1 as result|||That worked. Wonder why the first one didnt. Thanks!

Originally posted by Paul Young
try:

declare @.temp1 bit,
@.var1 varchar(10),
@.var2 varchar(10),
@.var3 varchar(10)
select @.var1='testing'
, @.var2='testing2'
, @.temp1=0
Select @.temp1 = case when @.var1 = @.var2 then 1 else 0 end
select @.temp1 as result|||By default Varchar (or Char, NChar, NVarchar, etc.) is a 1 character string. Adding the (10) makes it a 10 byte string.

No comments:

Post a Comment