Convert getdate() to int
Asked Answered
C

2

10

When I run the following query:

select convert(int, getdate())

I get the result:

-----------
41238

(1 row(s) affected)

Does anyone knows what does this mean?

Curtiscurtiss answered 26/11, 2012 at 18:39 Comment(5)
Allright! Found the answer already. It's the days. SELECT dateadd(day, convert(int, getdate()), '01/01/1900')Curtiscurtiss
No it means the days since 1/1/1900.Cryoscopy
Yep! But it depends on what DBMS you're using, right? Or all of them start counting at 1/1/1900?Curtiscurtiss
Depends, they could be but not likely. Ask us what you are really looking for and we can provide a solution, or are you just playing with convert()?Cryoscopy
Actually I was searching how to convert int to datetime. One thing led to another...Curtiscurtiss
C
8

Its the number of days since I think 1/1/1900, sql-server keeps the number of days since then.

Try dividing that number by roughly 365. You should get the value back in years (112). Since 1900 + 112 = 2012

Cryoscopy answered 26/11, 2012 at 18:42 Comment(0)
G
2

This is because SQL natively keeps the number of days since 01/01/1900

The decimal after the integer value is the time.

32.5 would equal 02/01/1900 12:00 pm

If you are looking to get and work with only part of the date as an integer I would recommend using datepart

This statement would return only the month and convert that value to an integer.

select convert(int, datepart(mm,getdate())
Glimp answered 26/11, 2012 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.