select Count(*) with date with sql server 6.5 and 7 
Author Message
 select Count(*) with date with sql server 6.5 and 7

I'm writing queries in VB that will be executed with SQL Server 6.5 and I'm
new to SQL Server.

I precisely want to count how many records have been created since the
past twelve months, and group the results by month.
With MS Access I could write :
SELECT COUNT(*) FROM tablename WHERE ...
AND tablename.datefieldname >= Dateadd('m',-13,Now())
AND tablename.datefieldname <= Dateadd('m',-1,Now())
GROUP BY MONTH(tablename.datefieldname), YEAR(tablename.datefieldname)

But it doesn't work in SQL for SQL SERVER.
I know that I have to use functions like Datepart instead of Year or
Month, and Getdate() instead of Now(). Am I wright ?
But there is also the problem of format, and the problem of making a
difference between two dates.

So I don't know how to write this type of queries.
Am I the only one one person having this problem ?
If you can help me solving this problem, and finding information on, or
examples of SQL Server queries, thanks a lot.

David.



Fri, 09 Aug 2002 03:00:00 GMT  
 select Count(*) with date with sql server 6.5 and 7
You need to use month instead of 'm' and getdate() instead of now().  Thats
all.



Quote:
> I'm writing queries in VB that will be executed with SQL Server 6.5 and
I'm
> new to SQL Server.

> I precisely want to count how many records have been created since the
> past twelve months, and group the results by month.
> With MS Access I could write :
> SELECT COUNT(*) FROM tablename WHERE ...
> AND tablename.datefieldname >= Dateadd('m',-13,Now())
> AND tablename.datefieldname <= Dateadd('m',-1,Now())
> GROUP BY MONTH(tablename.datefieldname), YEAR(tablename.datefieldname)

> But it doesn't work in SQL for SQL SERVER.
> I know that I have to use functions like Datepart instead of Year or
> Month, and Getdate() instead of Now(). Am I wright ?
> But there is also the problem of format, and the problem of making a
> difference between two dates.

> So I don't know how to write this type of queries.
> Am I the only one one person having this problem ?
> If you can help me solving this problem, and finding information on, or
> examples of SQL Server queries, thanks a lot.

> David.



Fri, 09 Aug 2002 03:00:00 GMT  
 select Count(*) with date with sql server 6.5 and 7
Hi David,
    You were pretty close. Try this one.

SELECT COUNT(*) FROM tablename WHERE ...
AND tablename.datefieldname >= Dateadd(mm,-13,getdate())
AND tablename.datefieldname <= Dateadd(mm,-1,getdate())
GROUP BY datepart(mm,tablename.datefieldname),
datepart(yyyy,tablename.datefieldname)

Thanks
Suresh Krishnan


Quote:
> I'm writing queries in VB that will be executed with SQL Server 6.5 and
I'm
> new to SQL Server.

> I precisely want to count how many records have been created since the
> past twelve months, and group the results by month.
> With MS Access I could write :
> SELECT COUNT(*) FROM tablename WHERE ...
> AND tablename.datefieldname >= Dateadd('m',-13,Now())
> AND tablename.datefieldname <= Dateadd('m',-1,Now())
> GROUP BY MONTH(tablename.datefieldname), YEAR(tablename.datefieldname)

> But it doesn't work in SQL for SQL SERVER.
> I know that I have to use functions like Datepart instead of Year or
> Month, and Getdate() instead of Now(). Am I wright ?
> But there is also the problem of format, and the problem of making a
> difference between two dates.

> So I don't know how to write this type of queries.
> Am I the only one one person having this problem ?
> If you can help me solving this problem, and finding information on, or
> examples of SQL Server queries, thanks a lot.

> David.



Fri, 09 Aug 2002 03:00:00 GMT  
 select Count(*) with date with sql server 6.5 and 7
When using Dateadd, the first argument is not in parens... and you have the
value slightly wrong.

You can use:   dateadd(mm, -13,getdate())

and GROUP BY datepart(mm, datefield), datepart(yy, datefield)

BTW, SQL 7 has a MONTH() and YEAR() function.

HTH

--
Kalen Delaney
MCSE, SQL Server MCT, MVP
www.InsideSQLServer.com
Feed Someone for Free Today:
     www.TheHungerSite.com


Quote:
> I'm writing queries in VB that will be executed with SQL Server 6.5 and
I'm
> new to SQL Server.

> I precisely want to count how many records have been created since the
> past twelve months, and group the results by month.
> With MS Access I could write :
> SELECT COUNT(*) FROM tablename WHERE ...
> AND tablename.datefieldname >= Dateadd('m',-13,Now())
> AND tablename.datefieldname <= Dateadd('m',-1,Now())
> GROUP BY MONTH(tablename.datefieldname), YEAR(tablename.datefieldname)

> But it doesn't work in SQL for SQL SERVER.
> I know that I have to use functions like Datepart instead of Year or
> Month, and Getdate() instead of Now(). Am I wright ?
> But there is also the problem of format, and the problem of making a
> difference between two dates.

> So I don't know how to write this type of queries.
> Am I the only one one person having this problem ?
> If you can help me solving this problem, and finding information on, or
> examples of SQL Server queries, thanks a lot.

> David.



Fri, 09 Aug 2002 03:00:00 GMT  
 select Count(*) with date with sql server 6.5 and 7
Look at YearPart and MonthPart functions in SQL server.


Quote:
> I'm writing queries in VB that will be executed with SQL Server 6.5 and
I'm
> new to SQL Server.

> I precisely want to count how many records have been created since the
> past twelve months, and group the results by month.
> With MS Access I could write :
> SELECT COUNT(*) FROM tablename WHERE ...
> AND tablename.datefieldname >= Dateadd('m',-13,Now())
> AND tablename.datefieldname <= Dateadd('m',-1,Now())
> GROUP BY MONTH(tablename.datefieldname), YEAR(tablename.datefieldname)

> But it doesn't work in SQL for SQL SERVER.
> I know that I have to use functions like Datepart instead of Year or
> Month, and Getdate() instead of Now(). Am I wright ?
> But there is also the problem of format, and the problem of making a
> difference between two dates.

> So I don't know how to write this type of queries.
> Am I the only one one person having this problem ?
> If you can help me solving this problem, and finding information on, or
> examples of SQL Server queries, thanks a lot.

> David.



Sat, 24 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. select Count(*) with date with sql server 6.5 and 7

2. select Count(*) with date with sql server 6.5 and 7

3. SELECT IN SQL-SERVER 6.5

4. Select first 10 records in SQL Server 6.5?

5. Inserting date into sql server 6.5

6. Null date, VB4 and SQL Server 6.5

7. Inserting date into sql server 6.5

8. New Transact-SQL Debugger for Microsoft SQL Server 6.5/7.0

9. Import text file in SQL Server 6.5 via SQL in Visual Basic

10. SQL Debugger for Microsoft SQL Server 7.0 and 6.5

11. Import text file in SQL Server 6.5 via SQL in Visual Basic

12. rerieving date from SQL 6.5 with century also

 

 
Powered by phpBB® Forum Software