Pivot table and Find DateDifference in fomat of Number of year,Month and Days in sql server

—-Use of datedifference

Declare @PreviousDate datetime=‘2001-11-08’;

Declare @CurrentDate date=CONVERT(date,GETDATE(),110);

Declare @CurrentDate1 date=CONVERT(varchar(10),GETDATE(),110);

declare @datediff int=DATEDIFF(DAY,@PreviousDate,@CurrentDate);

Declare @NumLeapYear INT=DATEDIFF(YEAR,@PreviousDate,@CurrentDate)/4 –for Count of Leap Year but may be some extend to be need.;

with FindDate as(Select @datediff as TotalDays,

(

 

@datediff % 365)as NumMonth,–for calculate of MonthCount

@CurrentDate

as TodayDate,@CurrentDate1 as TodayDate1 )

Select

 

TotalDays/365 asYear,

NumMonth

/31 asMonth,

(((NumMonth/31)%31)+@NumLeapYear)asdays,TotalDays as TotalDays,@CurrentDate as TodayDate,@CurrentDate1 as TodayDate1

from FindDate;

–Use of pivot table

select

 

*from

(

 

select permissionid,permissionname,parentpermissionid,level,isparent,dependentpermissionid,sortorder from Permission)as s

pivot

(

count

 

(permissionid)for [level] in(“1”,“2”,“3”,“4”))as d;

 

select

 

*from permission;

 

select

 

*from

(

 

select permissionid,[level] from Permission)as s

pivot

(

count

 

(permissionid)for [level] in(“1”,“2”,“3”,“4”))as d;

Leave a Reply