2

I want to select "up_year" with SQL case expression.

if I do this its ok:

SELECT CASE WHEN column1  = '2020' then 'up_year' else 'past_year' else end ;

but when trying to do like this : do not want to change every year.

SELECT 
CASE WHEN column1  = (select extract(year from sysdate) from dual) 
then 'up_year' 
else 'past_year' else end ;
3
  • 1
    You don't need "select ... from dual" in the right-hand side of the equation. Write it as "case when column1 = extract(year from sysdate)" Commented Oct 22, 2020 at 13:33
  • What does PL/SQL have to do with this? What you have shown is plain SQL. Please understand that PL/SQL is not another name for "Oracle SQL". Also: plain SQL only has case expressions, not case statements, and indeed everything in your post is case expressions. (CASE statements do exist - in PL/SQL!) I will edit your post to make these corrections; if I misunderstood, you can change back. Commented Oct 22, 2020 at 13:37
  • 1
    Most importantly: What is your question, really? Are you getting an error, and you don't understand why? That has nothing to do with the sub-SELECT; rather, you have an incorrect "else" right before "end". You already have the correct "else" in the expression. You would still be better off not using a sub-SELECT, but that is not what is causing an error in your current statement. Commented Oct 22, 2020 at 13:40

2 Answers 2

3

You can use with clause, for example

  with y as (select extract(year from sysdate) year from dual)
  SELECT CASE WHEN column1  = y.year then 'up_year' else 'past_year'  end from y -- and your table

A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement

You can separate to two steps with select into a variable the extract output and then use it

Sign up to request clarification or add additional context in comments.

5 Comments

thank you but can i do like a=select extract(year from sysdate) , and then select a from dual ;
@ÖmerFaruk you can also select into to store in a variable
select into is not needed; the OP is not doing anything in PL/SQL, despite what he seems to suggest. It all looks like plain SQL.
@mathguy I added because he asked and wrote PLSQL in first letters of title
Understood - which is why I edited his post to remove all references to PL/SQL. The entire original post made it pretty clear that it had nothing to do with PL/SQL.
2

You simply want to match for the current year. Then one option would be using TO_CHAR(,'yyyy') :

SELECT CASE
         WHEN column1 = TO_CHAR(sysdate,'yyyy') then
          'up_year'
         ELSE
          'past_year'
       END AS "My Year"
  FROM tab   

2 Comments

This is almost right. However, if we give the OP the benefit of the doubt and we assume that the data type of COLUMN1 is NUMBER (since he is comparing to a number in his attempt), then we should compare to a number. You could wrap your right-hand side within TO_NUMBER, but EXTRACT (YEAR FROM...) is more direct.
Thanks @mathguy , I also thought previously(to wrap within TO_NUMBER()), but considering the quoted value('2020') within the query, I didn't want to mess it up with TO_NUMBER()(seems as if redundant)

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.