Author |
Topic |
homerjay80
Starting Member
26 Posts |
Posted - 2007-05-03 : 17:06:33
|
Hello,I have a working query that outputs the first day of the month after the 1 year annviersary of given date:Select SSN ,CONVERT(CHAR(10),CONVERT(DATETIME,RTRIM(CONVERT(CHAR(2),MONTH(DATEADD(MM,1,DATEADD(YY,1,Date))))) + '/1/' +RTRIM(CONVERT(CHAR(4),YEAR(DATEADD(MM,1,DATEADD(YY,1,Date)))))),101) as StartFROM Tables.. Now what I would like to do is add a where clause that narrows dates that are less than today.I triedSelect SSN ,CONVERT(CHAR(10),CONVERT(DATETIME,RTRIM(CONVERT(CHAR(2),MONTH(DATEADD(MM,1,DATEADD(YY,1,Date))))) + '/1/' +RTRIM(CONVERT(CHAR(4),YEAR(DATEADD(MM,1,DATEADD(YY,1,Date)))))),101) as StartFROM Tables..where Start <= GetDate() but but it says my column name start is invalid.How should I go about doing this?Thanks |
|
homerjay80
Starting Member
26 Posts |
Posted - 2007-05-03 : 18:10:14
|
Looks like I figured it out. You have to repeat your alias expression in the where clause.Select SSN ,CONVERT(CHAR(10),CONVERT(DATETIME,RTRIM(CONVERT(CHAR(2),MONTH(DATEADD(MM,1,DATEADD(YY,1,Date))))) + '/1/' +RTRIM(CONVERT(CHAR(4),YEAR(DATEADD(MM,1,DATEADD(YY,1,Date)))))),101) as StartFROM Tables..whereCONVERT(CHAR(10),CONVERT(DATETIME,RTRIM(CONVERT(CHAR(2),MONTH(DATEADD(MM,1,DATEADD(YY,1,Date))))) + '/1/' +RTRIM(CONVERT(CHAR(4),YEAR(DATEADD(MM,1,DATEADD(YY,1,Date)))))),101) <= getdate() |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-03 : 18:45:57
|
wow that's a lot of converts.Try this instead:select dateadd(d, -Day(Date), dateadd(m, dateadd(yy, Date, 1), 1)) as Startfrom yourTablethis shows the principle so play with this to get it right for your requirement.Converts are slow and should be avoided.Also to use the alias in the where you can use a subquery:select SSN, Startfrom (your upper query here) t1where start <= getdate()_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-03 : 19:14:32
|
i think i'll print those links to your functions jeffand tape them to a wall _______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
|
|