Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to count data from sql
It's ok, but there is a problem.

My data is like this on my table:

coldate (varcharmax) (data comes datetimepicker short format)
23.02.2010
30.04.2010
15.04.2010

How do I count by month from sql?

I tried this: select * from datepart(month, coldate)=04

but it doesnt work.

I need to count by month and to add result to label1.text

What should I do?
Posted
Updated 25-Oct-10 5:10am
v2
Comments
Dalek Dave 25-Oct-10 11:10am    
Edited for Grammar and Readability.
Yusuf 25-Oct-10 11:28am    
What are you using varchar for your date? Why can't you use Date?

You are probably looking for GROUP BY and something to differentiate between months in different years.

This solution covers both scenarios (where you care about the year, and where you don't): SQL Hacks Solution[^]

Cheers.
 
Share this answer
 
v2
How about this...

If I have a table called Table1 with the following structure:

CurrentDate
2/5/2010
3/6/2009
3/7/2008
4/5/2010
6/5/2010
7/5/2009
2/7/2001
2/9/2010


and I run the following SQL Statement:
SQL
SELECT Part1.MyMonth, Count(Part1.MyMonth) AS CountOfMyMonth
FROM (SELECT Month(CurrentDate) AS MyMonth
      FROM Table1) AS Part1
GROUP BY Part1.MyMonth;


I get:

MyMonthCountOfMyMonth
23
32
41
61
71
 
Share this answer
 
select
Substring(coldate,7,4) + '-' + Substring(coldate,4,2),
COUNT(*)
from
TableName
group by
Substring(coldate,7,4) + '-' + Substring(coldate,4,2)
order by 1
 
Share this answer
 
I am not sure but, we dont have to use group by... why we use? I need to know for example 05 (may) how many times repeat? Two times, hundres times... i need one value, that is count...

My problem is, how i get, may from that value 30.05.2010...


William thanx for answer but i dont want to each month mod...

my table context product orders...

I want to learn; how many product ordered "january, february, march, april" in "2010"?


in your table
2/5/2010
3/6/2009
3/7/2008
4/5/2010
6/5/2010
7/5/2009
2/7/2001
2/9/2010


first value day or month?

and i seperate mark is point for my date format...

day.month.year

i need to count product order by month...
 
Share this answer
 
select month(cast(coldate as datetime))  as [month], count(*)
from tablename
group by month(cast(coldate as datetime))
order by [month] asc
 
Share this answer
 
I solved my problem by my friend Vladimir.

The answer is:

"SELECT count(*) as x from files where (substring(datein,4,2)='01' OR substring(datein,4,2)='02') and substring(datein,7,4)='2010'"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900