SQL date code question

  • Good evening,

    Not sure if this is the correct forum to post this in as I'm farely new to SQL. My question is more about SQL Teradata code and it seems like it'd be a fairly simple answer, but I can't seem to figure it out. below is a segment of the SQL code. What I'm trying to figure out is how to ONLY pull data that is between the first day of the previous month to previous day. I've got the previous day figured out, but am having problems with coding the first day of previous month. Any help is appreciated. . And apologies if this question doesn't belong here.

    Select vm.visit_date BETWEEN (first day of previous month) AND CURRENT_DATE -1

  • Maybe something like this?

    SELECT *

    FROM vm

    WHERE vm.visit_date >= DATEADD(month, DATEDIFF(month, 0, DATEADD(month, -1, CURRENT_TIMESTAMP)), 0) AND

    vm.visit_date < CURRENT_TIMESTAMP-1;

    CURRENT_TIMESTAMP and GETDATE() are the T-SQL functions to get today's date.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • or something like:

    SELECT *

    FROM vm

    WHERE vm.visit_date between DATEADD(DAY,1,EOMONTH(CURRENT_TIMESTAMP,-2)) AND

    CURRENT_TIMESTAMP-1;

    Pramod
    SQL Server DBA | MCSE SQL Server 2012/2014

    in.linkedin.com/in/pramodsingla/
    http://pramodsingla.wordpress.com/

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

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