1

I am new to SQL coding and I am encountering only one error code so far. At the second line WHEN CONVERT:

(rd.254 is my table date column)

CASE WHEN CONVERT(date,rd.F254) Between 
CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN rd.F64 END AS QtyLastWeek

WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN rd.F64 END AS Qty2WeeksAgo

*Screenshot Here*

I am trying to find and fix the syntax.

2
  • "rd.254 is my table date column" Commented Feb 6, 2019 at 13:53
  • 3
    Only tag the DBMS you are using ! Either MySQL or SQL-Server. Do not include links. Instead edit your question and include your error statement in the question. Commented Feb 6, 2019 at 13:53

3 Answers 3

2

you have missing comma and case

CASE WHEN CONVERT(date,rd.F254) Between CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN 
   rd.F64 
END AS QtyLastWeek,
CASE WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN 
   rd.F64 
END AS Qty2WeeksAgo
Sign up to request clarification or add additional context in comments.

Comments

2

There is incorrect syntax. If you want to select 2 columns QtyLastWeek and Qty2WeeksAgo you have to use the comma after QtyLastWeek and use another CASE expression.

SELECT 
    CASE WHEN CONVERT(date,rd.F254) BETWEEN CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN rd.F64 END AS QtyLastWeek,
    CASE WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN rd.F64 END AS Qty2WeeksAgo

Comments

0

You are missing case keyword in the second case statement. Try this !

CASE WHEN CONVERT(date,rd.F254) Between CONVERT(date,getdate()-7) AND CONVERT(date,getdate()-1) 
THEN rd.F64 
END AS QtyLastWeek,
CASE   WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) 
      THEN rd.F64 
END AS Qty2WeeksAgo

Comments

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.