r/SQL • u/Substantial-Book2534 • Sep 25 '26
Discussion Using DateAdd for timestamps
I am making updates to some older reports and come across this line (or similar) often:
DateAdd(hh, 0, GetDate())
Why do you do that? Is there a legitimate reason why you couldn’t just write GetDate()?
7
Upvotes
11
u/DamienTheUnbeliever Sep 25 '26
date functions are one of those unfortunate areas where database products are hideously variable, so we would need to know which product you're working with here.
I could imagine that there could be a system where such a call would truncate the timestamp and no longer include minutes, seconds, or subcomponents of seconds.
But in all likelihood it's a mistake, that's been copied and perpetuated by people who don't care to understand but just want to "get things done"
4
u/dafugiswrongwithyou Sep 25 '26
Where have you seen this? To me, that just seems like someone in your orgs past understood DateAdd(hh, SomeNum, GetDate()), didn't realise it was two seperate functions used in tandem, and that was their kludge for getting the current date. I've never seen it in the wild before.
4
u/da_chicken Sep 25 '26
That looks similar to code used to truncate a datetime in SQL Server. That can be useful if you need to aggregate a table by the date or date and hour.
Before SQL Server supported the date data type, using
DATEADD(dd, DATEDIFF(dd, 0, YourDateColumn), 0)was frequently used to remove the time from a datetime field.1
u/KBradl Sep 25 '26
I have never seen that or done it. They values are the same so it seems superflous.
Maybe its one of those things people do to make things look more complex. I once ran into some code that had If 1 = 1 all over the place.
2
u/vaakezu Sep 25 '26
The only reason for this would be if you are an hour away from GMT and someone hardcoded DST then forgot about it.
1
u/Eleventhousand Sep 25 '26 edited Sep 25 '26
They were probably testing something, and it was just easier to swap out a single digit than a line of code. I wouldn't worry about seeing things like this unless its constant.
But, you should just ask your teammates or supervisor. Build relationships with your coworkers. You're not going to get as good of a reason asking complete strangers why an unknown person wrote a piece of code. Maybe there was an emergency with reports with standard dates being inclusive of its boundaries needing to be exclusive and the entire code base had to be changed dynamically by end of day.
1
u/Legitimate-Pin-8619 Sep 26 '26
That's almost certainly a no-op. DATEADD(hh, 0, GETDATE()) returns the same datetime as GETDATE() — you're adding zero hours. I've mostly seen it come from report builders or copy-pasted snippets where someone always wrapped "current time" in DATEADD and left the offset at 0.
What *is* useful is the related truncate pattern: DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) (or CAST(GETDATE() AS date) on newer SQL Server) to strip the time portion. If you just need "now", use GETDATE() / SYSDATETIME() and drop the DATEADD wrapper.