Getdate with previous date

  • I’m trying to figure out the SQL Server statement to find the system date for the previous day. I’m currently using a where statement with this syntax: “where shipdate is between getdate ()-1 and getdate ()”. Will that result in finding a shipdate between dd/mm/yyyy 00.00.00 and dd/mm/yyyy 23.59.59 for the previous day?

  • WHERE shipdate >= DATEADD(d,DATEDIFF(d,'19000101',GETDATE())-1,'19000101')

    AND shipdate < DATEADD(d,DATEDIFF(d,'19000101',GETDATE()),'19000101')

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • remember getdate returns the time portion of NOW: 2010-03-26 12:18:05.390

    you want to start at midnite yesterday, not 12:18 of yesterday.

    and another way:

    --2010-03-25 00:00:00.0002010-03-25 23:59:59.997

    select DATEADD(dd, DATEDIFF(dd,0,getdate()), -1),

    DATEADD(ms,-3,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))

    WHERE shipdate

    BETWEEN DATEADD(dd, DATEDIFF(dd,0,getdate()), -1)

    AND DATEADD(ms,-3,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (3/26/2010)


    remember getdate returns the time portion of NOW: 2010-03-26 12:18:05.390

    you want to start at midnite yesterday, not 12:18 of yesterday.

    and another way:

    --2010-03-25 00:00:00.0002010-03-25 23:59:59.997

    select DATEADD(dd, DATEDIFF(dd,0,getdate()), -1),

    DATEADD(ms,-3,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))

    WHERE shipdate

    BETWEEN DATEADD(dd, DATEDIFF(dd,0,getdate()), -1)

    AND DATEADD(ms,-3,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))

    Lowell, it would be better to use an open interval range check instead of between. It could also cause problems if you are using SQL Server 2008 and the new datetime data type which has a higher precision than 3 milliseconds.

    If you look at mister.magoo's post you'll see how he did that.

    Also, be very careful changing the date part of the datediff calculation. It is better (IMHO) to subtract the number of days from the results of the datediff instead of using a fixed date. In other words:

    DATEADD(day, DATEDIFF(day, 0, getdate()) - 1, 0) instead of DATEADD(day, DATEDIFF(day, 0, getdate()), -1)

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • i always just use left(getdate(),11). so i would just use

    select * from datestable

    where mydate between left(getdate() -1,11) and left(getdate(),11)

  • Animal Magic (3/27/2010)


    i always just use left(getdate(),11). so i would just use

    select * from datestable

    where mydate between left(getdate() -1,11) and left(getdate(),11)

    This is one of the slowest methods available. First, you are relying on implicit conversion of the datetime to a specific representation. Second, this returns a string, that is then implicitly converted back to a datetime. Third, between is inclusive so you would include rows for the current day where the time is midnight. Probably not what is intended.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • also, isn't the implicit conversion of left(getdate() -1,11) dependent on the servers regional settings? I think that makes an assumption on the regional settings that might not be true for everyone.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Jeffrey Williams-493691 (3/26/2010)


    Lowell, it would be better to use an open interval range check instead of between. It could also cause problems if you are using SQL Server 2008 and the new datetime data type which has a higher precision than 3 milliseconds. If you look at mister.magoo's post you'll see how he did that.

    I would agree with that, as a general approach. Subtracting 3 millisconds is technically fine with DATETIME, of course.

    Also, be very careful changing the date part of the datediff calculation. It is better (IMHO)...

    Why?

    ...to subtract the number of days from the results of the datediff instead of using a fixed date. In other words:

    DATEADD(day, DATEDIFF(day, 0, getdate()) - 1, 0) instead of DATEADD(day, DATEDIFF(day, 0, getdate()), -1)

    In the spirit of being compatible with the new data types in SQL Server 2008, you should avoid using the subtraction operator directly with dates and times. It only works with DATETIME. Use DATEADD instead. In fact, always use specific date/time manipulation functions where possible.

    WHERE ship_date >= DATEADD(DAY, -1, DATEADD(DAY, DATEDIFF(DAY, CONVERT(DATETIME, '19690711', 112), CURRENT_TIMESTAMP), CONVERT(DATETIME, '19690711', 112))),

    AND shipdate < DATEADD(DAY, DATEDIFF(DAY, CONVERT(DATETIME, '19690711', 112), CURRENT_TIMESTAMP), CONVERT(DATETIME, '19690711', 112));

    Paul

    edit: tags

  • Paul,

    If you look at the calculation - it is not performing a calculation on getdate(), but rather on the results from the datediff calculation.

    I know you disagree with using 0 as the seed date in this calculation - and understand the reasoning for that. I don't like using a different seed date, mostly because it only works for days and I like having something that works for all.

    For example:

    DECLARE @seedDate datetime;

    SET @seedDate = '20100101';

    SELECT DATEADD(day, DATEDIFF(day, @seedDate, GETDATE()) - 1, @seedDate);

    SELECT DATEADD(day, DATEDIFF(day, @seedDate, GETDATE()) - 0, @seedDate);

    SELECT DATEADD(day, DATEDIFF(day, @seedDate, GETDATE()) + 1, @seedDate);

    SELECT DATEADD(month, DATEDIFF(month, @seedDate, GETDATE()) - 1, @seedDate);

    SELECT DATEADD(month, DATEDIFF(month, @seedDate, GETDATE()) - 0, @seedDate);

    SELECT DATEADD(month, DATEDIFF(month, @seedDate, GETDATE()) + 1, @seedDate);

    SELECT DATEADD(year, DATEDIFF(year, @seedDate, GETDATE()) - 1, @seedDate);

    SELECT DATEADD(year, DATEDIFF(year, @seedDate, GETDATE()) - 0, @seedDate);

    SELECT DATEADD(year, DATEDIFF(year, @seedDate, GETDATE()) + 1, @seedDate);

    Instead of adding another calculation around this to add or subtract days, all we are doing adjusting the number of days difference to get the calculation.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams-493691 (3/28/2010)


    If you look at the calculation - it is not performing a calculation on getdate(), but rather on the results from the datediff calculation.

    So it is. Sneaky bracket! I had to look more than once to see it still. Oh well. Sorry about that.

  • Jeffrey Williams-493691 (3/28/2010)


    I don't like using a different seed date, mostly because it only works for days and I like having something that works for all.

    That code seems to produce the same results regardless of the seed date value, which I would expect. It is 5:40am here and I am very tired...am I missing your point here (again)?

  • Paul White NZ (3/28/2010)


    Jeffrey Williams-493691 (3/28/2010)


    I don't like using a different seed date, mostly because it only works for days and I like having something that works for all.

    That code seems to produce the same results regardless of the seed date value, which I would expect. It is 5:40am here and I am very tired...am I missing your point here (again)?

    No, you are not missing anything - that is the intent. What is important is the calculation does not depend on changing the seed date. It depends on how you add/subtract to the results of the datediff.

    Where the seed date is important is determining which day of the month/year you get. If you want the end of the month all the time, you set the seed date as an end of the year seed (e.g. '20091231').

    If you want the 15th of the month, set the seed to the 15th. It works this way for anything above the day increment. For days and less, the seed date doesn't matter at all - just set one and the calculations work.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Paul White NZ (3/28/2010)


    Jeffrey Williams-493691 (3/28/2010)


    If you look at the calculation - it is not performing a calculation on getdate(), but rather on the results from the datediff calculation.

    So it is. Sneaky bracket! I had to look more than once to see it still. Oh well. Sorry about that.

    Yeah - it is. Took me a while to realize that also...

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Thanks, Jeffrey.

  • Viewing 14 posts - 1 through 13 (of 13 total)

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