Friday 20 January 2017

Get Name of days between tow dates in sql server



//           Create function


Create FUNCTION [dbo].[Get_NameOf_Days](
@startdate datetime
,@enddate datetime
,@DayValue int -- return the value of days (starting day with sunday- value=1)
,@NameValue int -- return the name of days (starting day with monday- value=0)
)
RETURNS VARCHAR(MAX)
BEGIN
declare @count int =0
    while @startdate<=@enddate
    Begin
        IF DatePart(WEEKDAY,@startdate) = @DayValue
            SET @count=@count+1
        SET @startdate=DateAdd(d,1,@startdate)
    END
RETURN  (select DATENAME(dw,@NameValue)+' : '+CAST(@count as varchar))

END


//           Call  function

select dbo.Get_NameOf_Days('2017-1-1','2017-1-31',1,6)



//           Result


1 comment:

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...