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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Manipulating dates using Ints

Author  Topic 

ShawnM
Starting Member

2 Posts

Posted - 2004-08-09 : 15:38:48
Hello,

I have a query that needs to determine someones Age based on 3 columns in a database; 3 int columns that contain the month, day, and year of their birth, respectivlely. I guess I can't use DateDiff since the colmun isn't of the value type datetime. Any way to convert these three colmuns to a date time and compare it to the current date and return an new colmumn using AS?

Thanks

-- shawn

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-08-09 : 15:54:36
[code]create table t ( d int, m int, y int)

insert into t
select 1,1,1970 union all
select 2,1,1960

select
datediff
(
year,
convert(datetime, convert(varchar(2),d) + '/' + convert(varchar(2),m) + '/' + convert(varchar(4),y)),
getdate()
) as age_in_years
from t

drop table t[/code]
Go to Top of Page

Pat Phelan
Posting Yak Master

187 Posts

Posted - 2004-08-09 : 16:01:24
You'll get bitten if you don't take into account whether the anniversary has occured yet this year! I'd use something like:
--  ptp  20040205  Find the number of @pcUnits between two datetime values

CREATE FUNCTION dbo.fTimeBetween(
@pcUnit VARCHAR(10)
, @pdBegin DATETIME
, @pdEnd DATETIME
) RETURNS INT
AS BEGIN

DECLARE
@iGuess INT
, @dTest DATETIME

IF @pcUnit LIKE '%d%'
BEGIN
SELECT @iGuess = DateDiff(day, @pdBegin, @pdEnd)
SELECT @dTest = DateAdd(day, @iGuess, @pdBegin)
END
ELSE IF @pcUnit LIKE '%m%'
BEGIN
SELECT @iGuess = DateDiff(month, @pdBegin, @pdEnd)
SELECT @dTest = DateAdd(month, @iGuess, @pdBegin)
END
ELSE IF @pcUnit LIKE '%h%'
BEGIN
SELECT @iGuess = DateDiff(hour, @pdBegin, @pdEnd)
SELECT @dTest = DateAdd(hour, @iGuess, @pdBegin)
END
ELSE
BEGIN
SELECT @iGuess = DateDiff(year, @pdBegin, @pdEnd)
SELECT @dTest = DateAdd(year, @iGuess, @pdBegin)
END


RETURN CASE WHEN @pdEnd < @dTest THEN @iGuess - 1 ELSE @iGuess END
END
GO

SELECT fTimeBetween('Year', Convert(datetime, Str(y, 4) + '-' + Str(m, 2) + '-' + Str(d, 2), GetDate())
-PatP
Go to Top of Page

Pat Phelan
Posting Yak Master

187 Posts

Posted - 2004-08-09 : 16:27:29
That should get you lots closer, but it doesn't like leap years.

-PatP
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-08-09 : 16:32:14
quote:
Originally posted by Pat Phelan

That should get you lots closer, but it doesn't like leap years.



Among other things.. :) would not tell me today is my Birthday.
Go to Top of Page

Pat Phelan
Posting Yak Master

187 Posts

Posted - 2004-08-09 : 16:39:01
Well then: Happy Birthday!

-PatP
Go to Top of Page

ShawnM
Starting Member

2 Posts

Posted - 2004-08-09 : 17:42:26
LoL... what are you doing answering posts on your birthday?!

Thanks for your help - that is AWESOME and is exactly what I was looking for.

Thanks again.

-- shawn
Go to Top of Page
   

- Advertisement -