简体   繁体   中英

Conditional Select Statement in T-SQL

I have a table INFO with 3 columns as NAME, ID and FLAG. The condition for select statement is if NAME column is not null it should display the NAME based on the ID and FLAG values from WHERE clause.

My original query was as follows,

Select NAME from INFO where ID=101 and FLAG='Y'

This query returns the NAME if it is present for given ID and FLAG condition. But shows NULL if NAME is not present for the given ID and FLAG condition (As it should as per the T-SQL properties)

In my case I want to execute the query only if NAME column value if NOT NULL. Till now I have used CASE condition but it does not skips the execution

select CASE NAME is not null then NAME 
else 'NA' end from INFO where ID=101 and FLAG='Y'

This only replace the resultant NULL with NA, Is there a way I can execute this select only if NAME column has values

Why not add an

and name is not null

to the where clause?

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