0

I'm asked to select some columns from a table. One of which does not exist in the table. This column is called PayComment and I need to use a conditional to check if there is data in another separate column inside that table. If there is PayComment will say 'Yes' If there isn't it will say 'no'.

I'm confused on how to do this because I've only ever created a column using an alias([OldColumn] AS [NewColumn]) or declared it as a variable and INSERT it into the table. However I have to create this column/use a conditional to determine what's inside it, while keeping it in the select statement whilst I grab the other existing columns.

I know this is wrong but this is what I'm imagining

CREATE PROC spPayIncreaseListing
AS
    SELECT FirstName, LastName, CONVERT(varchar, HireDate, 1) AS HireDate, PayRate, 
    CONVERT(varchar, StartDate, 1) AS PayRateStartDate, CONVERT(varchar, EndDate, 1) AS PayRateEndDate, 
    PayComment AS IF Work.COLA > 0
                        PayComment = 'Yes';
                  ELSE  
                        PayComment = 'NO';

enter image description here

7
  • 1
    You want a case expression Commented Apr 10, 2020 at 1:57
  • Just edited it now Commented Apr 10, 2020 at 2:03
  • @DaleK Here's what I'm expecting Commented Apr 10, 2020 at 2:07
  • The 'PayComment' column needs to be created Commented Apr 10, 2020 at 2:08
  • @Dale Im going to try the CASE expression Commented Apr 10, 2020 at 2:09

1 Answer 1

1

For what you are asking for this would be the case expression you would need:

CREATE PROC spPayIncreaseListing
AS
    SELECT FirstName, LastName, CONVERT(varchar, HireDate, 1) AS HireDate, PayRate, 
    CONVERT(varchar, StartDate, 1) AS PayRateStartDate, CONVERT(varchar, EndDate, 1) AS PayRateEndDate, 
    CASE WHEN Work.COLA > 0 THEN 'Yes' else 'No' END AS PayComment
Sign up to request clarification or add additional context in comments.

1 Comment

case expression not statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.