Ask Your Question
3

How to round up a date in SQL server?

asked 2022-06-07 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-02 20:00:00 +0000

lalupa gravatar image

To round up a date in SQL Server, you can use the DATEADD and DATEDIFF functions to add a certain number of days to the original date based on the rounding interval and then use the FLOOR function to truncate any remaining time after the new date has been calculated. Here's an example to round up a date to the nearest month:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0) -- add one month and set day to 1 OR SELECT CONVERT(date, DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))) -- set last day of month

This will return the first date of the next month. If you want to round up to a different interval such as year, week, etc., simply replace the MONTH parameter in the DATEADD and DATEDIFF functions with the desired interval.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-07 11:00:00 +0000

Seen: 8 times

Last updated: Dec 02 '21