简体   繁体   中英

SQL Server switch data to display something else

I am trying to figure out I can change the data on the fly.

This is my current query:

SELECT [EmployeeTC_No] AS "Employee TC#"
      ,[pye_nlast] AS "Name Last"
      ,[pye_nfirst] AS "Name First"
      ,[Dept] AS "Department"
      ,[pye_status] AS "Active"
      ,[HireDate] AS "Hire Date"
      ,[SeparationDate] AS "Separation Date"
  FROM [testing].[dbo].[testing]

In the column of pye_status the data comes in as "A" or "T" and I want it to be "1" or "0".

I have tried to add a case statement trying to do some kind of switch but nothing appears to even get me close.

CASE/WHEN should do the trick:

,CASE WHEN [pye_status] = 'A' THEN 1 
WHEN [pye_status] = 'T' THEN 0 
ELSE NULL END AS "Active"

Or a simple case/when will work too:

, CASE [pye_status]
WHEN 'A' THEN 1 
WHEN 'T' THEN 0
ELSE NULL END AS "Active"

Use CASE expression or IIF() function as

CASE WHEN [pye_status] = 'A' THEN 1 ELSE 0 END AS [Active]

OR

IIF([pye_status] = 'A', 1, 0) AS [Active] 

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