Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get maximum time limit of getdate()

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2008-07-24 : 04:15:01
how to set todays maximum timestamp to getdate() function

eg:getdate()
Ans :2008-07-24 13:16:48.727 ----Current
i want to get 2008-07-24 23:59:59.727

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-07-24 : 04:16:15
quote:
Originally posted by aoriju

how to set todays maximum timestamp to getdate() function

eg:getdate()
Ans :2008-07-24 13:02:48.727
i want to get 2008-07-24 24:59:59.727




are you sure that's what you want?

Em
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-07-24 : 04:17:16
but still.... why milliseconds of .727 ???

Em
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-24 : 04:21:14
[code]SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000101 23:59:59.997'),
DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000102')[/code]
If you are going to compare the date with a column in a table like this

select * from table1 where column1 <= {above first result}

you are better off with using the second result like this

select * from table1 where column1 < {above second result}

That way, you are sure that all records for today are fetched, and your code will work on SQL Server 2008 as well.
Pre SQL Server 2008 has a time resolution of 3/1000th of a second. That's why 997 milliseconds are the last time for a day.

That is going to change in SQL Server 2008. There you can have a resolution of a microsecond Yes, then the time part will end with .999999

So use the second alternative above to compare dates.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-07-24 : 04:29:31
in 2008 i think the .999999 is only relevant to the seperate TIME datatype and DATETIME2. DATETIME is still acurate to .997

Em
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2008-07-24 : 04:30:47
Not worry about millisecond ....i want last time of today in getdate function()
quote:
Originally posted by Peso

SELECT	DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000101 23:59:59.997'),
DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000102')

If you are going to compare the date with a column in a table like this

select * from table1 where column1 <= {above first result}

you are better off with using the second result like this

select * from table1 where column1 < {above second result}

That way, you are sure that all records for today are fetched, and your code will work on SQL Server 2008 as well.
Pre SQL Server 2008 has a time resolution of 3/1000th of a second. That's why 997 milliseconds are the last time for a day.

That is going to change in SQL Server 2008. There you can have a resolution of a microsecond Yes, then the time part will end with .999999

So use the second alternative above to compare dates.



E 12°55'05.25"
N 56°04'39.16"


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-24 : 04:45:59
quote:
Originally posted by aoriju

Not worry about millisecond ....i want last time of today in getdate function()
quote:
Originally posted by Peso

SELECT	DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000101 23:59:59.997'),
DATEADD(DAY, DATEDIFF(DAY, '19000101', getdate()), '19000102')

If you are going to compare the date with a column in a table like this

select * from table1 where column1 <= {above first result}

you are better off with using the second result like this

select * from table1 where column1 < {above second result}

That way, you are sure that all records for today are fetched, and your code will work on SQL Server 2008 as well.
Pre SQL Server 2008 has a time resolution of 3/1000th of a second. That's why 997 milliseconds are the last time for a day.

That is going to change in SQL Server 2008. There you can have a resolution of a microsecond Yes, then the time part will end with .999999

So use the second alternative above to compare dates.



E 12°55'05.25"
N 56°04'39.16"





Peso explained well on how to use it in a query
Why do you want to know the last time of the day?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-07-24 : 09:38:52
Jeez, people. Maybe he has a legit reason for wanting this:
select dateadd(millisecond, -2, dateadd(day, datediff(day, 0, getdate()), 0))

e4 d5 xd5 Nf6
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-07-24 : 09:49:14
quote:
Originally posted by blindman

Jeez, people. Maybe he has a legit reason for wanting this:
select dateadd(millisecond, -2, dateadd(day, datediff(day, 0, getdate()), 0))

e4 d5 xd5 Nf6



That returns the last time for yesterday.


select TodaysLastTime = dateadd(day,datediff(day,0,getdate()),'23:59:59.997')

Results:
TodaysLastTime
------------------------------------------------------
2008-07-24 23:59:59.997

(1 row(s) affected)


CODO ERGO SUM
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-07-24 : 10:43:07
So nit-picky. I was only off by one day, which is pretty dang close considering that the Earth is billions of years old, don'tcha think?
What I meant to post was this:
select dateadd(millisecond, -2, dateadd(day, datediff(day, 1, getdate()), 0))

e4 d5 xd5 Nf6
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-07-24 : 10:46:34
don't be ridiculous! 0 is 19000101 so how can it possible be billions of years old!?

Em
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-07-24 : 11:49:26
That is all the proof I needed. Thanks.

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -