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.

Post History

80%
+6 −0
Q&A How are integers interpreted in contexts that expect a date?

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: D...

1 answer  ·  posted 4y ago by ajv‭  ·  edited 4y ago by Alexei‭

Question sql tsql sql-server
#2: Post edited by user avatar Alexei‭ · 2020-10-24T14:57:32Z (about 4 years ago)
added tag
  • 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.
  • 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.
#1: Initial revision by user avatar ajv‭ · 2020-08-24T21:02:10Z (about 4 years ago)
How are integers interpreted in contexts that expect a date?
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.