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
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
SELECT CASE install_Date WHEN NULL THEN ''
ELSE Install_Date
END AS install_Date
FROM device
;)
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
ELSE Install_Date
END AS install_Date
FROM device
ASKER
hi,
i actually used the one suggested by xiong and did some little changes. the actual statement is:
SELECT CASE convert(varchar(11),[insta ll 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
i actually used the one suggested by xiong and did some little changes. the actual statement is:
SELECT CASE convert(varchar(11),[insta
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
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)
ELSE convert(char(11), [Install Date], 105)
Or you can use varchar(11).
ELSE convert(varchar(11), [Install Date], 105)
ELSE convert(varchar(11), [Install Date], 105)
ASKER
thanks guys for all your time.
ann
ann
WHEN NOT NULL THRN Install_Date
END AS install_Date
FROM device
cheers