Home > other >  Conditional Select Statement in T-SQL
Conditional Select Statement in T-SQL

Time:06-28

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

CodePudding user response:

Why not add an

and name is not null

to the where clause?

  • Related