Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How are integers interpreted in contexts that expect a date?

+6
−0

I found a confusing construction in several stored procs in an MS SQL 2008 R2 database:

DATEADD(dd, 0, DATEDIFF(dd, 0, some_date))

As I understand it, these are the relevant function signatures:

DATEDIFF(datepart, startdate, enddate)
DATEADD(datepart, number, date)

That is, the proc supplies 0 as the startdate argument to DATEDIFF. The return value from DATEDIFF is an int, which in turn is supplied as the date argument to DATEADD. And the code does work -- or at least it runs without error.

So:

  1. How is 0 (or other integers) interpreted in contexts that expect a date?

  2. (bonus) What on earth did the author intend this to do? That DATEADD should be a no-op, however the integer is interpreted.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+8
−0

SQL Server uses '1900-01-01' as a "zero-point" in DATEDIFF(dd, 0, some_date):

select DATEDIFF(dd, 0, '1900-01-01') --> 0 
select DATEDIFF(dd, 0, GETDATE())    --> 44066 days since the "zero-day"

The whole expression is used to strip time from the DATETIME and still have it as a DATETIME (as opposed to DATE). An alternative would be to CAST it to DATE, but it will change the type:

select CAST(GETDATE() AS DATE)  --> 2020-08-25 (no time)

What is more interesting is that the minimum DATETIME is not '1900-01-01', but 1753-01-01 which of course corresponds to a negative integer value:

select cast(-53690 as datetime)

Relevant resources:

SQL Server function to return minimum date from Stack Overflow.

Best approach to remove time part of datetime in SQL Server from Stack Overflow.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)

Sign up to answer this question »