Link to home
Create AccountLog in
Avatar of wala_lang
wala_lang

asked on

using if condition in a transact-sql statement

hi,

how do i go about in including an if condition in my select statement?

i would like something like the one below:

select iif(install_date = 'Jan 1 1990',"",install_date) from device

sort of something like the isnull function.

thanks

ann
Avatar of xiong8086
xiong8086

SELECT CASE install_Date WHEN NULL THEN ''
                                       WHEN NOT NULL THRN Install_Date
            END AS install_Date
FROM   device

cheers
SELECT CASE install_Date WHEN NULL THEN ''
                                       ELSE Install_Date
            END AS install_Date
FROM   device

;)
SELECT CASE install_Date WHEN 'Jan 1 1990' THEN ''
                                       ELSE Install_Date
            END AS install_Date
FROM   device
Avatar of wala_lang

ASKER

hi,

i actually used the one suggested by xiong and did some little changes.  the actual statement is:

SELECT CASE convert(varchar(11),[install Date]) WHEN 'Jan  1 1900' THEN null
                                       ELSE [Install Date]
            END AS [Installed Date]
FROM   device

i want to display the [install date] in dd-MMM-yyyy format.  how will i do that? i will just increase the points for this additional question.

thanks

ann
ASKER CERTIFIED SOLUTION
Avatar of xiong8086
xiong8086

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
select replace(convert( varchar(11), nullif( install_date, 'Jan 1 1990'), 113), ' ', '-') AS [Installed Date]
Hope you get the picture

EG
if exists(select t.account_number from tiecapital T join
universal_interest_rate A on T.account_number = A.account_number)
   
Print 'Records already Exists'
else
do something else

if (select install_date from device where install_date
='Jan 1 1990'
Print something
else
do something


Try this on the ELSE condition of your Case statement..

ELSE convert(char(11), [Install Date], 105)
Or you can use varchar(11).

ELSE convert(varchar(11), [Install Date], 105)
thanks guys for all your time.

ann