SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using DateAdd and Convert
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pinto
Aged Yak Warrior

United Kingdom
590 Posts

Posted - 01/18/2007 :  10:46:26  Show Profile  Reply with Quote
Here's my sp which doesn't do what I want

I want it to only list records with LastDateIn 10 days prior to today's date.

CREATE Procedure [dbo].[spRMU_NoFilesBookedInByDay]
AS

SELECT Convert(nvarchar, LastDateIn,103) AS Expr1, COUNT(Status) AS Expr2
FROM tblFiles
where Convert(datetime, LastDateIn,103) < dateadd(day, -10, getdate())
GROUP BY Convert(nvarchar, LastDateIn,103)
ORDER BY Convert(nvarchar, LastDateIn,103) DESC

GO

SwePeso
Patron Saint of Lost Yaks

Sweden
30249 Posts

Posted - 01/18/2007 :  10:52:07  Show Profile  Visit SwePeso's Homepage  Reply with Quote
select dateadd(day, datediff(day, 0, getdate()), 0), count(*) from tblFiles
where lastdatein < dateadd(day, datediff(day, 10, getdate()), 0)
group by dateadd(day, datediff(day, 0, getdate()), 0)


Peter Larsson
Helsingborg, Sweden

Edited by - SwePeso on 01/18/2007 10:54:00
Go to Top of Page

Pinto
Aged Yak Warrior

United Kingdom
590 Posts

Posted - 01/18/2007 :  11:01:55  Show Profile  Reply with Quote
I am getting the error below when I check the syntax of the sp

Error 164 : GROUP BY expressions must refer to column names that appear in the select list.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30249 Posts

Posted - 01/18/2007 :  11:09:45  Show Profile  Visit SwePeso's Homepage  Reply with Quote
My bad

select dateadd(day, datediff(day, 0, lastdatein), 0), count(*) from tblFiles
where lastdatein < dateadd(day, datediff(day, 10, getdate()), 0)
group by dateadd(day, datediff(day, 0, lastdatein), 0)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Pinto
Aged Yak Warrior

United Kingdom
590 Posts

Posted - 01/18/2007 :  11:33:22  Show Profile  Reply with Quote
Thank you. I have amended your sql a bit as I what I wanted was the last 10 days from now - sorry I did not explain myself properly.

This is what I have now and it works fine.

CREATE Procedure [dbo].[spRMU_NoFilesBookedInByDay]
AS

select dateadd(day, datediff(day, 0, lastdatein), 0), count(*) from tblFiles
where lastdatein > dateadd(day, datediff(day, 10, getdate()), 0)
group by dateadd(day, datediff(day, 0, lastdatein), 0)
order by dateadd(day, datediff(day, 0, lastdatein), 0) desc


GO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30249 Posts

Posted - 01/18/2007 :  11:49:20  Show Profile  Visit SwePeso's Homepage  Reply with Quote
>= will the best choice.

Thank you.



Peter Larsson
Helsingborg, Sweden
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000