need 3rd day of Calendar month - date precision to exclude minutes

  • I need query (in my case an INSERT) to run only if today's date is 3rd day of Calendar Month?

    how to get date precisions for to exclude minutes on right side of the IF?


     IF     CONVERT(VARCHAR(10), GETDATE(), 112)   =    cast(DATEADD(month, DATEDIFF(month, 0, getdate()), 2) AS smalldatetime)
     SELECT 1 + 1
     ELSE
     select 0

    how to get date precisions for to exclude minutes on right side of the IF?

    --Quote me

  • polkadot - Sunday, December 16, 2018 11:14 AM

    I need query (in my case an INSERT) to run only if today's date is 3rd day of Calendar Month?

    how to get date precisions for to exclude minutes on right side of the IF?


     IF     CONVERT(VARCHAR(10), GETDATE(), 112)   =    cast(DATEADD(month, DATEDIFF(month, 0, getdate()), 2) AS smalldatetime)
     SELECT 1 + 1
     ELSE
     select 0

    how to get date precisions for to exclude minutes on right side of the IF?

    You should have a look at the functionality of DATEPART. 


         IF DATEPART(dd,GETDATE()) = 3
     SELECT 1 + 1
       ELSE
     SELECT 0
    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • For more info on DATEPART, please see the article at the following link.
    https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017

    There's also a wealth of knowledge just a click away in the menu on the left of that article. 

    There are also a ton of "date/time" tricks that you can do with dates and times like finding the first day of the given month as provided by GETDATE() or some data stored in a column.  Since temporal data manipulation is so very important in databases, I strongly recommend that you learn what all of the "Date and Time Functions" are in SQL Server and mostly memorize them.  The link I provided is just scratching the surface of what you can actually do.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

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