Need T-SQL to indicate if getdate() = Sunday

  • In search of T-SQL which will accomplish this: Given "any" date, tell me if this is a SUNDAY.

    Example:

    Declare @mydate datetime

    Select @mydate = (Select getdate())

    IF @mydate = 'Sunday' .....

    thx in advance

    BT
  • use Datepart and be sure you know what your datefirst setting is. You can set any day to be the first day of the week.

    I think you can also get the day name with datepart as well.

  • when you use datepart and dw as the argument, it returns the weekday as integer value. where

    Sunday=1 and Saturday=7 as mentioned in BOL. I hope the below query helps.

    Declare @mydate datetime

    Select @mydate = (Select getdate())

    select datepart(dw,@mydate)

    IF (datepart(dw,@mydate) = 1)

    SELECT 'ITS A SUNDAY'

    else

    SELECT 'IT IS NOT A SUNDAY'

  • hmmm -- isn't your repsonse analagous to something like this:

    Do you have a recipe for a chocolate cake?

    Yes, mix ingrediants and cook it. hahahaha (need specifics and preferrably an example please)

    I'm trying to leverage an experts T-SQL experience - where they may have this in their toolkit already.

    BT
  • if datename(weekday, getdate()) = 'Sunday'

  • thanks Grasshopper -- I modified your code to look like this:

    DECLARE@CheckDate DATETIME, @DayOfWeek SMALLINT

    SELECT @CheckDate = '2008-07-06 00:00:00.000'

    SELECT @DayOfWeek = (SELECT datepart(weekday, @CheckDate))

    SELECT @DayOfWeek As TheDay

    -- 1=Sunday

    BT

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply