简体   繁体   中英

SQL VARIABLE RENAME IN QUERY

I have a table called ORDPAY, a field in the table is called Paid_Flag. I want my query to show me something in replace of the actual paid_Flag value. For exmaple if paid_Flag = 1 I want the Query output to display 'Cash', if paid_Flag = '2' i want the Query OUtput to display 'Check'. How can i code this into my select statement results

Like Gordon said, the CASE expression is what you need. It would look something like this

SELECT CASE WHEN paid_Flag = 1 THEN 'Cash'
            WHEN paid_Flag = 2 THEN 'Check'
            ELSE 'unknown'
       END AS newPaidFlag
FROM <yourTable>

How about this:

Select Case Paid_Flag
            When 1 then 'Cash'
            When 2 then 'Check'
            Else 'Error'
       End output
From ORDPAY

You don't need CAST , all what you need is CASE :

DECLARE @ORDPAY TABLE ( ID INT , Paid_Flag INT);

INSERT INTO @ORDPAY VALUES
(1,1) , (2,2);

SELECT ID,
Paid_Flag = CASE Paid_Flag
            When 1 then 'Cash'
            When 2 then 'Check'
END
FROM @ORDPAY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM