This function will return numbers of weekends between two dates using datepart function in sql.
CREATE FUNCTION NumberOfWeekEnds(@dFrom
DATETIME,
@dTo DATETIME)
RETURNS INT AS
BEGIN
-- DECLARE @dFrom DATETIME
-- DECLARE @dTo DATETIME
Declare
@weekends int
Set
@weekends = 0
While @dFrom
<= @dTo Begin
If ((datepart(dw, @dFrom) = 1) OR (datepart(dw, @dFrom) = 7))
Set @weekends = @weekends + 1
Set @dFrom = DateAdd(d, 1, @dFrom)
End
Return (@weekends)
END
GO
SELECT NumberOfWeekEnds('2/27/2013','3/10/2013')
Datepart function in sql
--Get year part
of date use datepart year, yyyy, yy
SELECT DATEPART(year,GETDATE())
--Get quarter
part of date use datepart quarter, qq, q
SELECT DATEPART(quarter,GETDATE())
--Get month part
of date use datepart month, mm, m
SELECT DATEPART(month,GETDATE())
--Get day of
year part of date use date part dayofyear, dy, y
SELECT DATEPART(dayofyear,GETDATE())
--Get day of
month part of date use date part day, dd, d
SELECT DATEPART(day,GETDATE())
--Get weak of
year part of date use datepart week, wk, ww
SELECT DATEPART(week,GETDATE())
--Get weekday of
week part of date use datepart weekday, dw
SELECT DATEPART(weekday,GETDATE())
--Get hour of
day in 24 hour format part of date use datepart hour, hh
SELECT DATEPART(hour,GETDATE())
--Get minute of
hour part of date use datepart minute, n
SELECT DATEPART(minute,GETDATE())
--Get second of
minute part of date use datepart second, ss, s
SELECT DATEPART(ss,GETDATE())
--Get
millisecond of second part of date use datepart millisecond, ms
SELECT DATEPART(millisecond,GETDATE())