python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
convert mb to gb in sql server

T-SQL CASE Expressions

The CASE expressions compare an expression to a set of simple expressions to determine the result and CASE can be used in any statement that allows a valid expression.

It is IF-THEN-ELSE statement.

Syntax: For a simple CASE expression
CASE COLL
      WHEN 1 THEN 'One' 
      WHEN 2 THEN 'Two'  
      WHEN 3 THEN 'Three' 
  ELSE 'None' 
END AS COLL_Name 
-- CREATE TABLE
CREATE TABLE [dbo].[Tbl_Demo](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](500) NULL,
    [Age] [int] NULL,
    [IsActive] [bit] NULL,
    [IsDeleted] [bit] NULL,
 CONSTRAINT [PK_Tbl_Demo] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
--INSERT TABLE ROWS
SET IDENTITY_INSERT [dbo].[Tbl_Demo] ON 

GO
INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES (1, N'Anil Singh', 30, 1, 0)
GO
INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES (2, N'Aradhya', 3, 1, 0)
GO
INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES (3, N'Reena', 28, 1, 0)
GO
INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES (4, N'Sunil Singh', 27, 1, 0)
GO
INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES (5, N'Sushil', 22, 1, 0)
GO
SET IDENTITY_INSERT [dbo].[Tbl_Demo] OFF
GO

--SELECT WITH CASE AND THE RESULT LOOKS LIKE
SELECT Name, 
    CASE Name 
        WHEN 'Aradhya'  THEN 'Aradhya Singh'
        WHEN 'Reena'    THEN 'Reena Singh' 
        WHEN 'Sushil'   THEN 'Aradhya Singh' 
        ELSE Name 
    END AS Full_Name
FROM [dbo].[Tbl_Demo]

--THE RESULT LOOKS LIKE
Name    Full_Name
---------------------------
Anil Singh  Anil Singh
Aradhya Aradhya Singh
Reena   Reena Singh
Sunil Singh Sunil Singh
Sushil  Aradhya Singh
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions