Selecting data from a table based on date

I have a database table like this

+-------+--------------+----------+
|   id  |    ip        |    date  |
+-------+--------------+----------+
|   505 |192.168.100.1 |2010-04-03|
|   252 |192.168.100.5 |2010-03-03|
|   426 |192.168.100.6 |2010-03-03|
|   201 |192.168.100.7 |2010-04-03|
|   211 |192.168.100.10|2010-04-03|
+-------+--------------+----------+

      

how can i recover data from this table where month = 03, how can i write sql for this.

select * from table where month=03 

      

something like that.

+2


source to share


4 answers


If you are using mysql use the MONTH () function I think.



select * from table where month(date) = 3

      

+2


source


select * from table where month(date) = 3

      

If you are looking for more than 2010-03 it would be advisable to do it like this:



select * from table where date >= '2010-03-01' AND date < '2010-04-01'

      

As this query will use the index if available

+1


source


SELECT * FROM Table WHERE month(date) = 3

      

OR using datepart Function:

SELECT * FROM Table WHERE datepart(mm, date)=3

      

+1


source


As an alternative

select * from table where '2010-02-31' < date < '2010-04-01' 

      

must work.

0


source







All Articles