SQL Server How do I get the last possible time of a particular day

gpnt7bae  于 2023-06-04  发布在  其他
关注(0)|答案(5)|浏览(363)

I'm trying to achieve the last possible time of a particular day eg for Date of 2008-01-23 00:00:00.000 i would need 2008-01-23 23:59:59.999 perhaps by using the dateadd function on the Date field?

ct3nt3jp

ct3nt3jp1#

The answer is SELECT DATEADD(ms, -3, '2008-01-24') , the explanation is below.

From Marc's blog :
But wait, Marc... you said you like to use BETWEEN , but that query doesn't have one... that's because BETWEENis inclusive , meaning it includes the end-points. If I had an Order that was due at midnight of the first day of the next month it would be included. So how do you get the appropriate value for an end-of-period? It's most certainly NOT by using date-parts to assemble one (but is you must, please remember that it's 23:59:59.997 as a maximum time... don't forget the milliseconds). To do it right, we use the incestuous knowledge that Microsoft SQL Server DATETIME columns have at most a 3 millisecond resolution (something that is not going to change). So all we do is subtract 3 milliseconds from any of those end-of-period formulas given above. For example, the last possible instant of yesterday (local time) is:

SELECT DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd, 0, GetDate()), 0))

So to do the orders due this month as a BETWEEN query, you can use this:

SELECT [ID]
   FROM [dbo].[Orders]
   WHERE [ShipDue] BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GetUTCDate()), 0)
   AND DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GetUTCDate()) + 1, 0))

Remember, always make sure that you do math against input parameters, NOT columns, or you will kill the SARG -ability of the query, which means indexes that might have been used aren't.

wyyhbhjk

wyyhbhjk2#

SELECT DATEADD(ms, -2, DATEADD(dd, 1, DATEDIFF(dd, 0, GetDate())))

I thought you had c# at first.. I will leave this here in case anyone else stumbles across this.

DateTime now = DateTime.Now;
DateTime endofDay = now.Date.AddDays(1).AddMilliseconds(-1);

You can replace the 'now' variable with whatever day you are trying to figure out

4ktjp1zp

4ktjp1zp3#

Add -1 milliseconds to the start of the next day (DateAdd even supports nanoseconds, if you want to get real fine).

But most likely you just want to use this value in a comparison, and in that case it's even simpler.

Rather than something like this:

AND @CompareDate <= [LastTimeforThatday]

or this:

@compareDate BETWEEN [StartDate] AND [LastTimeforThatday]

Do it like this:

AND @CompareDate < [BeginningOfNextDay]

or this:

AND (@CompareDate >= [StartDate] AND @CompareDate < [BeginningOfNextDay])
xtfmy6hx

xtfmy6hx4#

Why back into it?

SELECT DATEADD(ms, 86399997, *yourDate*)
aor9mmx1

aor9mmx15#

I was able to use:

select {fn curdate()} + ' 23:59:59.000'

or

select DATEADD(ss,-1,DATEADD(DAY,1,CAST({fn curdate()} as DATETIME)))

to get the end of a day

相关问题