Sample Code for CASE function in Sql Server 2005

Here is the sample code for using CASE function in Sql Server 2005.

--@Test is a sample Table variable which has 3 columns

declare @Test table
(
Description varchar(50),
[Type] varchar(50),
Status bit
)


-- Adding new rows
INSERT INTO @Test VALUES ('Money Deposit',1,1)
INSERT INTO @Test VALUES ('Money Getting',0,1)
INSERT INTO @Test VALUES ('Buying Mutual Funds',1,0)


--Extracting Records
SELECT
'Description'=[Description] ,
'Status'= CASE [Status]
WHEN 0 THEN 'DEBIT'
WHEN 1 THEN 'CREDIT'
END
FROM
@Test



select [Description] as 'Description',Status,[Type] from @Test


-Sample TSQL

declare @x int
set @x = 1

SELECT
'TimeZone'=
CASE @x
WHEN 0 THEN 'Debiting the account'
WHEN 1 THEN 'Crediting the account'
END

No comments: