简体   繁体   中英

Case statements in my sql select statement

SELECT
column1,
column2,
(CASE WHEN column1=NULL THEN @NEW_column =column2
WHEN column1!=NULL THEN @NEW_column =column1
END) AS NEW_column,
FROM my_table

In the above select statement , i am creating a new variable called
NEW_column and the value of it should be based on the conditions in case statement. When i run this query value is "NEW_column" is returned as NULL. Can anyone help me solve this logical error?

You need to write column1 is null similarly column1 is not null

DEMO

SELECT
   column1,
   column2,
   CASE WHEN column1 is NULL THEN column2
   WHEN column1 is not NULL THEN column1
   END AS NEW_column,
FROM my_table

OUTPUT:

col1    col2    newcol
3       null    3
null    4       4
5      null     5

It means you don't get any value with your conditions

There are several way to debug the your SQL statement:

sample

SELECT
column1,
column2,
ISNULL(column1,column2) AS NEW_column 
FROM my_table

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