Lucas_Santos
Lucas_Santos

Reputation: 4740

TSQL to return NO or YES instead TRUE or FALSE

How can I show a different value if some columns return FALSE,

for example,

COLUMN "BASIC" returns FALSE, but I need show for the user YES or NO. Case FALSE, return NO.

Upvotes: 25

Views: 65980

Answers (6)

FLICKER
FLICKER

Reputation: 6683

since SQL 2012 you can use IIF function

IIF(BASIC = 'TRUE', 'YES', 'NO')

Upvotes: 8

edilio
edilio

Reputation: 1868

You could create a user defined function so in the future you can just say something like:

select dbo.YesNo(Active) from APAccount

here is the function

CREATE FUNCTION [dbo].YesNo(@Value Bit)
    RETURNS varchar(3)
    BEGIN
      DECLARE @R as varchar(3)

      SET @R =
        (
        Select 
            case 
                when @Value = 1 then 'Yes'
                else 'No'
            end
        )
       RETURN @R
    END

Upvotes: 0

Mark Kram
Mark Kram

Reputation: 5832

Like this:

SELECT TipoImovel_Id AS TII_SEQ, Descricao AS TII_DSC, Sigla AS TII_DSC_SIGLA, case when basic = 'FALSE' then 'NO' else 'YES' end 
FROM San_TipoImovel

Upvotes: 0

gbn
gbn

Reputation: 432331

If varchar or bit, handling NULLs

case
    when BASIC = 'FALSE' then 'NO'
    when BASIC <> 'FALSE' then 'YES'
    else 'UNDEFINED'
end

or if just bit

case
    when BASIC = 1 then 'YES'
    when BASIC = 0 then 'NO'
    else 'UNDEFINED'
end

Edit:

SELECT 
    TipoImovel_Id AS TII_SEQ,
    Descricao AS TII_DSC, 
    Sigla AS TII_DSC_SIGLA,
    -- choose which one you want from the answers here
    case
        when BASIC = 1 then 'YES'
        when BASIC = 0 then 'NO'
        else 'UNDEFINED'
    end AS SomeColumnName
FROM San_TipoImovel";

Upvotes: 51

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56172

Use CASE statement:

case BASIC when 'True' then 'Yes' ELSE 'No' end

Upvotes: 0

Derek
Derek

Reputation: 23248

case when column = 'FALSE' then 'NO' else 'YES' end

Upvotes: 6

Related Questions