Results 1 to 6 of 6

Thread: Iif use

  1. #1
    Join Date
    Jan 2003
    Posts
    8

    Unanswered: 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?
    Thanks

  2. #2
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    I belive yu are using IIF in the wrong context try:

    case when @var1 = @var2 then 1 else 0 end
    Paul Young
    (Knowledge is power! Get some!)

  3. #3
    Join Date
    Jan 2003
    Posts
    8
    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?

  4. #4
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    try:

    Code:
    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
    Paul Young
    (Knowledge is power! Get some!)

  5. #5
    Join Date
    Jan 2003
    Posts
    8
    That worked. Wonder why the first one didnt. Thanks!

    Originally posted by Paul Young
    try:

    Code:
    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

  6. #6
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    By default Varchar (or Char, NChar, NVarchar, etc.) is a 1 character string. Adding the (10) makes it a 10 byte string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •