Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Sunday, 25 December 2016

Case Statement In SQL Server

 No comments   

Introduction

In this article i will explain about How to implement or use Case statement in SQL Server. 

Practice

While working with an Player Record application their is an requirement to return Gender as Male or Female not in "M" or "F" format. Here i am sharing my code. I have save Gender as M or F type in my table, so based on the gender type i return Full gender type name.



Declare @Gender varchar(50);

SET @Gender = (SELECT TOP 1 GenderType FROM PL_PhysicalDesc
        WHERE PlayerID = 101) 
         SET @Gender = case @Gender  when  'M'   Then  'Male' 
                                                     Else 'Female' 
                END   
Print @Gender


I shared my scenario with you but you can use Case statement by this way in any of your condition. 


Order By cause with Case Statement



SELECT * FROM dbo.Student
ORDER BY 
 CASE WHEN Gender='M' THEN FirstName END Desc,
 CASE WHEN Gender='F' THEN LastName END ASC



Having Clause with Case Statement



SELECT  FirstName 
               ,Gender
               , Total=MAX(Fee) 
                 FROM dbo.Student
                GROUP BY Gender,FirstName
                HAVING (MAX(CASE Gender WHEN  'M' 
                               THEN Fee
                                ELSE NULL END) > 180.00
                 OR MAX(CASE Gender WHEN  'F' 
                                THEN Fee
                                 ELSE NULL END) > 170.00)




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write