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 2005 Forums
 Transact-SQL (2005)
 SQL date

Author  Topic 

Kurmanc
Yak Posting Veteran

92 Posts

Posted - 2008-04-01 : 04:44:23
Hi,

How is the syntax written to get the date when a person turns into 18. For example a person born 19900101 will turn 18, 20080101. How can this be written with a select statement to recieve the result 20080101?

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-04-01 : 04:49:37
start with this, may be some one may provide better solution

SELECT REPLACE(CONVERT(VARCHAR(10), DATEADD(YEAR, 18, CAST('19900101' AS DATETIME)), 120), '-', '')
Go to Top of Page

Kurmanc
Yak Posting Veteran

92 Posts

Posted - 2008-04-01 : 04:53:10
Nice Job!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-01 : 05:16:52
Use Style 112 instead and there is no need to replace.
DECLARE	@dt VARCHAR(20)

SET @dt = '19900101'

SELECT CONVERT(CHAR(8), DATEADD(YEAR, 18, CAST(@dt AS DATETIME)), 112)
But why are you using varchar to store dates?



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

- Advertisement -