How to Use CHOOSE SQL function

CHOOSE() function 

CHOOSE() function is used to get the value of a list by its index number. it acts like an index into an array. The Index starts from 1.

Syntax:- CHOOSE ( index, val1, val2,........., valN ] )

Example:-


 SELECT
   EMP.NAME,
   CHOOSE(DATEPART(MM,SAL.DATE),'JANUARY','FEBRUARY','MARCH','APRIL'
,'MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER'
,'DECEMBER')[MONTH],
DATEPART(YYYY, SAL.DATE) AS [YEAR],
SAL.AMOUNT 
FROM SALARY SAL 
INNER JOIN EMPLOYEE EMP ON EMP.ID=SAL.EMPLOYEEID



Result:-


NAME               MONTH    YEAR          AMOUNT
-------------------------------------------------
Alok Kumar Singh   MAY          2016        50000.48
Tomas Paul         MAY          2016        54000.43
Cristomfer Dee     MAY          2016        456463.89
Niel Macengi       MAY          2016        150021.00
Alok Kumar Singh   JUNE         2016        450000.00
Alok Kumar Singh   OCTOBER      2016        45.00

How to Use CHOOSE SQL function How to Use CHOOSE SQL function Reviewed by Alok Singh on 01:39 Rating: 5
Powered by Blogger.