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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Date Select

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US

I am trying to pull records that are at least 7.5 years old but I can't get my stored procedure to work right. Can someone please help me? My stored procedure is as follows:

SELECT max(inout.inout_date) as 'lastdate', inout.account_number, loans.short_name from inout
INNER JOIN loans ON inout.account_number=loans.account_number
WHERE datediff(mm, 'lastdate',getdate()) >=111
GROUP BY inout.account_number, loans.short_name


The error message that I'm getting is:
Syntax error converting datetime from character string.

(inout.inout_date is a datetime field)
 
I think that 7.5 year = 90 months because 1 year has 12 months 7.5*12=90.
The following query works for me
SELECT max(i.inout_date) as 'lastdate',
i.account_number,
l.short_name
FROM inout i INNER JOIN loans l ON
i.account_number = l.account_number
WHERE datediff(mm, i.inout_date,getdate())>=90
GROUP BY i.account_number, l.short_name
AL Almeida
NT/DB Admin
"May all those that come behind us, find us faithfull"
 

Adding to Robert's explanation, T-SQL doesn't allow you to the alias later in the query as can be done in MS Access. 'Latest' is an alias and as AL Almeida showed in his query, you can use the date column, "i.inout_date" in the Datediff function. Terry L. Broadbent
Life would be easier if I had the source code. -Anonymous
 
Thanks to each of you for your help. Also, thanks for the math lesson. It's just been one of those days.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top