[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Replace a value in a SELECT statement

I am running a SELECT statement and would like to replace a value in the result set.
The value that is being replaced is an integer and the result will be a string.

Here is an example of the statement:

Use xtremeSQL
GO
SELECT CASE
WHEN [Supervisor ID] = '5' THEN 'John'
ELSE [Supervisor ID]
END AS Supervisor, SSN, Salary
FROM  Employee

Here is the error:

Conversion failed when converting the varchar value 'John' to data type int.

How do I write this?
0
mnoisette
Asked:
mnoisette
1 Solution
 
käµfm³d 👽Commented:
It sounds like the type of the Supervisor ID column is defined as int. You'll need to cast to keep the types consistent in your CASE:

SELECT CASE 
WHEN CAST([Supervisor ID] AS varchar(255)) = '5' THEN 'John'
ELSE CAST([Supervisor ID] AS varchar(255))
END AS Supervisor, SSN, Salary
FROM  Employee

Open in new window

0
 
deightonCommented:
SELECT CASE
WHEN [Supervisor ID] = 5 THEN 'John'
ELSE CAST([Supervisor ID] AS VARCHAR(20))
END AS Supervisor, SSN, Salary
FROM  Employee
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now