Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2002
    Posts
    192

    Unanswered: IF statement in SQL String

    MS Access allows for If statements to be used in SQL. Is there anyway to do something similar to the below line in SQL server?

    SELECT a, IIF (True = True, "This is True", "This is False") as b,c FROM tblTest;

    My attempt in SQL Server's query analyser goes like this but give me errors.

    SELECT a,
    IF True = True
    BEGIN
    "This is True" as b,
    END
    ELSE
    BEGIN
    "This is False" as b,
    END
    c
    FROM tblTest;

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    For using in batch or in stored procedure:

    declare @id int
    set @id=1
    if @id=1 begin
    select '@id=1'
    end
    else begin
    select '@id<>1'
    end

    In select you can use case:

    select case when getdate()<>getdate() then 'It is impossible!' else 'Ok' end as 'Result'

    Syntax
    Simple CASE function:

    CASE input_expression
    WHEN when_expression THEN result_expression
    [ ...n ]
    [
    ELSE else_result_expression
    ]
    END

    Searched CASE function:

    CASE
    WHEN Boolean_expression THEN result_expression
    [ ...n ]
    [
    ELSE else_result_expression
    ]
    END

    Good luck

Posting Permissions

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