Home > Blockchain >  How to make query with condition for case?
How to make query with condition for case?

Time:08-03

Right now I have this table in my DB:

table_book :
id (int),
author (varchar (50) ),
genre (varchar (50) ),
date (date),
country (varchar (50).

I wan't to make query like this:

select * from table_book 
case when date = null then select author
case when date > 1999-01-01 then select country

On first condition I would like get only author, on second condition I would like get only country (not all infos from table)

It's pretty simple example for query but I want to know how to do it on this way or it's impossible)

CodePudding user response:

if you want a result based on that condition than

select 
(case 
when date Is Null then author
when date > Convert(datetime, '1999-01-01' ) then country
else ''
end) as res
from table_book 

CodePudding user response:

You can try;

select (case when date Is Null then cast(author as varchar) when date > '1999-01-01' then cast(country as varchar) else '' end) as res from table_book

  • Related