On Thu, 20 Apr 2006 14:30:41 -0500, tumb <tu*********@mail.codecomments.com> wrote:
in <tu*********@mail.codecomments.com>
Could someone show me an example to find the age of a person using VB
script if we know the birthdate.
Thanks in advance
M
Here's some T SQL that does what you want. See if you can translate it.
It computes age as of a particular anniversary so just substitute the current
date for the anniversary date.
SET NOCOUNT ON
DECLARE @Anniversary SMALLDATETIME
SET @Anniversary =
CASE
WHEN MONTH(CURRENT_TIMESTAMP) = 12
THEN STR(YEAR(CURRENT_TIMESTAMP)) + '1201'
ELSE STR(YEAR(CURRENT_TIMESTAMP)-1) + '1201'
END
IF @Filter = 0
SELECT
E.FirstName AS [First Name],
E.LastName AS [Last Name],
E.Address1,
E.Address2,
CASE LEN(E.Zip) WHEN 6 THEN LEFT(E.Zip,3) + ' ' + RIGHT(E.Zip,3) ELSE E.Zip END AS Zip,
SUBSTRING(E.Phone1,1,3) + ' ' + SUBSTRING(E.Phone1,4,3) + ' ' + SUBSTRING(E.Phone1,7,4) AS Phone1,
SUBSTRING(E.Phone2,1,3) + ' ' + SUBSTRING(E.Phone2,4,3) + ' ' + SUBSTRING(E.Phone2,7,4) AS Phone2,
E.Email,
CASE E.Official WHEN 0 THEN '' ELSE 'Yes' END AS Official,
CASE Birthday
WHEN '1900-01-01'
THEN Age
ELSE LTRIM(STR(
CASE
WHEN DATEADD(year, DATEDIFF (year, Birthday, @Anniversary), Birthday) > @Anniversary
THEN DATEDIFF (year, Birthday, @Anniversary) - 1
ELSE DATEDIFF (year, Birthday, @Anniversary)
END))
END AS Age,
E.TaxID,
CASE Birthday WHEN 0 THEN '' ELSE CONVERT(VARCHAR(10),E.Birthday,111) END AS Birthday,
---
This posting is provided "AS IS" with no warranties and no guarantees either express or implied.
Stefan Berglund