SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DateAdd Function


DateAdd Function

Author
Message
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4382 Visits: 3668
Mike John (3/4/2009)
Alternative answer is :

2009-09-02 17:30:00.000 -- which is exactly what you will see if you are configured for a UK date format. ie the dateadds behave as described by the input date is in an ambiguous format... was it the 2nd Sept or 9th Feb?

Mike John

That's only an issue if the year is mentioned at the end (02-09-2009). When the year is mentioned first it's always yyyy-mm-dd, many times also noted without the dashes (20090902).

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28825 Visits: 9671
webrunner (3/4/2009)
***********SPOILER ALERT***********

john.arnott (3/3/2009)
For one additional degree of difficulty: What's the result from this query?

select DATEADD(HOUR,5,DATEADD(MINUTE,-59,'2009-02-09 12:34:56.789'))


a) 2009-02-09 07:34:57.789
b) 2009-02-09 16:35:56.789
c) 2009-02-09 16:33:56.790
d) 2009-02-09 16:35:56.790


I'm going to go with:
b) 2009-02-09 16:35:56.789

UPDATE: I ran the result and got c) 2009-02-09 16:33:56.790. (My original second choice.) Why do the milliseconds get rounded up?

Thanks,
webrunner


It's the limit of the precision in ms. If the last digit is not a possible value, it gets rounded up or down depending on the closest number.
Dietmar Weickert
Dietmar Weickert
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1620 Visits: 374
HanShi (3/4/2009)
Mike John (3/4/2009)
Alternative answer is :

2009-09-02 17:30:00.000 -- which is exactly what you will see if you are configured for a UK date format. ie the dateadds behave as described by the input date is in an ambiguous format... was it the 2nd Sept or 9th Feb?

Mike John

That's only an issue if the year is mentioned at the end (02-09-2009). When the year is mentioned first it's always yyyy-mm-dd, many times also noted without the dashes (20090902).


Well, that's exactly what I would have expected. Nevertheless, when I copy & pasted the given statement

SELECT DATEADD(MINUTE,30,DATEADD(hour,7,'2009-02-09 10:00:00:000'))



into SS Studio Express and pressed F5, my result was

2009-09-02 17:30:00.000



Rather confuses me... Unsure

Best regards,
Dietmar Weickert.
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9548 Visits: 9751
YYYY-xx-xx will be interpreted as

YYYY-MM-DD or YYYY-DD-MM depending on the connection's language setting


Far away is close at hand in the images of elsewhere.

Anon.


Shaun McGuile
Shaun McGuile
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 1060
David: we are going off topic.

I think we all agree that again we have a QoD which is not locale neutral.

This can cause issues with selecting a correct answer.

We need better questions. Smile

Hiding under a desk from SSIS Implemenation Work Crazy
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9548 Visits: 9751
David: we are going off topic.


Quite Ermm

I think we all agree that again we have a QoD which is not locale neutral.


Yep Wink

My post was not meant to cause offence or consternation in any way Blush


Far away is close at hand in the images of elsewhere.

Anon.


webrunner
webrunner
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4132 Visits: 3881
Ninja's_RGR'us (3/4/2009)


...
I'm going to go with:
b) 2009-02-09 16:35:56.789

UPDATE: I ran the result and got c) 2009-02-09 16:33:56.790. (My original second choice.) Why do the milliseconds get rounded up?

Thanks,
webrunner


It's the limit of the precision in ms. If the last digit is not a possible value, it gets rounded up or down depending on the closest number.


Thanks, but could you explain in more detail what happened? I still don't quite understand how the rounding works in this case.

For example, I ran this:
select DATEADD(MINUTE,-59,'2009-02-09 12:34:56.785')

and got this:
2009-02-09 11:35:56.787

I know SQL is doing something to get that result, but if anything I thought it would end in .786 in this case, yet it ends in .787.

Thanks again!
webrunner

-------------------
"I love spending twice as long and working twice as hard to get half as much done!" – Nobody ever.
Ref.: http://www.adminarsenal.com/admin-arsenal-blog/powershell-how-to-write-your-first-powershell-script

"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Chad Crawford
 Chad Crawford
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2830 Visits: 18717
webrunner,
.786 isn't a valid millisecond for the datetime datatype...

All datetimes end in either .xx0, .xx3 or .xx7
.xx9, .xx0 and .xx1 are rounded up/down to .xx0
.xx2, .xx3, .xx4 are rounded up/down to .xx3
.xx5, .xx6, .xx8 are rounded up/down to .xx7

Check out the BOL "Date and Time" for an example table.

Thanks,
Chad
webrunner
webrunner
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4132 Visits: 3881
Chad Crawford (3/4/2009)
webrunner,
.786 isn't a valid millisecond for the datetime datatype...

All datetimes end in either .xx0, .xx3 or .xx7
.xx9, .xx0 and .xx1 are rounded up/down to .xx0
.xx2, .xx3, .xx4 are rounded up/down to .xx3
.xx5, .xx6, .xx8 are rounded up/down to .xx7

Check out the BOL "Date and Time" for an example table.

Thanks,
Chad


Wow, that is odd (well, except for .xx0 Smile), but thank you so much for telling me. I never would have figured it out, and there it is spelled out in BOL.

Thanks again,
webrunner

-------------------
"I love spending twice as long and working twice as hard to get half as much done!" – Nobody ever.
Ref.: http://www.adminarsenal.com/admin-arsenal-blog/powershell-how-to-write-your-first-powershell-script

"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
notquitexena
notquitexena
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 313
I thought it was a dead giveaway that only one answer had the correct date prefix (2009-02-09). The wrong answers had 2009-02-10 and 2009-02-08. Except for clicking on the wrong button on mistake, I can't understand why anyone would miss this question.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search