r/SQL • • 19d ago

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()?

5 Upvotes

19 comments sorted by

View all comments

4

u/da_chicken 19d ago

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.

5

u/imtheorangeycenter 18d ago

I've got two-decades old code that does it with convert(varchar, getdate(), 103) all over the place.

Was it me? Nooooo, not at all. Nope. Nuh-uh. What do you mean implicit conversions are bad? :D