Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datefiff function - concern 2

Status
Not open for further replies.

victorashi

IS-IT--Management
Jul 22, 2006
32
0
0
RO
today i wanted to calculate the difference in years from the date of birth untill today ( how old i am )
so i used the datediff function :

select datediff (year, 1982-01-01, getdate())
(no column name)
---------------------
101
is that possible that i am 101 years old, or i didn`t use the function right , although ....
select getdate()
(no column name)
---------------------
2006-08-01 12:58:31.903

 
yes, you didn't use the function right

try using it with '1982-01-01' instead of 1982-01-01

:)



r937.com | rudy.ca
 
ok , a small mistake , but why did`n mr.SQL give me an error , he actualy did something, but i don`t know what.
 
Well, to explain.

Without the apostrophes, 1982-01-01 is evaluated as a numerical expression equal to 1980. Dates are stored internally as a floating point number with day 0 being Jan 1, 1900. Add 1980 days to Jan 1 1900 and you get June 4, 1905, which is 101 years ago.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I've been called many things, but genius......

It's important to understand how dates work in SQL. I encourage you to look at this Faq's: faq183-5842 to help you understand them a little better.

Thanks for the star.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top