To determine weekday or weekend using T-SQL

SQL Server UDF
create function fn_IsWeekDay
(
    @date datetime
)
returns bit
as
begin

    declare @dtfirst int
    declare @dtweek int
    declare @iswkday bit

    set @dtfirst = @@datefirst – 1
    set @dtweek = datepart(weekday, @date) – 1

    if (@dtfirst + @dtweek) % 7 not in (5, 6)
        set @iswkday = 1 –business day
    else
        set @iswkday = 0 –weekend

    return @iswkday
end

Usage:
if dbo.fn_IsWeekDay(@date) = 1
    select ‘WeekDay’
else
select ‘WeekEnd’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s