Extracting month from datetime data type

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Extracting month from datetime data type

Post by devork »

I know that it is very easy, have done it by some how
is there any easy to extract month from datetime
2003-01-01 01:01:02
the thing that I want to do is extract the records that are specific
month
select * from table where datetime_column=MONTH('2003-01-02')
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

you can do this:

Code: Select all

select * from table where SUBSTRING(datetime_column,6,2)=MONTH('2003-01-02');
have a look at http://www.mysql.com/doc/en/String_functions.html for further details
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

or just

Code: Select all

select * from table where MONTH(datetime_column)=MONTH('2003-01-02');
:)
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

problem resolved some how

Post by devork »

cybaf wrote:or just

Code: Select all

select * from table where MONTH(datetime_column)=MONTH('2003-01-02');
:)
first one is working [how i forgot that]
second one is returning NULL
thanx by the way
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

that's strange... I just tried it again on my server and it worked nicely...

well well the first one worked so...:)
Post Reply