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
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...
#2: Post edited
- 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
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.