Click to See Complete Forum and Search --> : SQL MONTH() does not exist? uhm?


mith36
06-30-2009, 02:56 PM
I am trying to use this query...

$query = "SELECT val FROM obs WHERE id='".$_GET['id']."' AND obsdate >= '".$hisdate."' AND obsdate <= '".$curdate."' AND MONTH(obsdate) = '".$mosel."' ";

... that translate to ...

SELECT val FROM obs WHERE id='GBYC2' AND obsdate >= '1969-06-01' AND obsdate <= '2009-06-01' AND MONTH(obsdate) = '01'


... to try and select records only of the month indicated by php var $mosel.

Everytime I try to use a month/date function on the obsdate column (which is of type date), I get a weird error...


ERROR: function month(date) does not exist LINE 1: ...e >= '1969-06-01' AND obsdate <= '2009-06-01' AND MONTH(obsd... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.


What's wrong here?

BOUND4DOOM
06-30-2009, 04:50 PM
I think you might need to specify the database your using. Microsoft SQL server this is somewhat valid. When I say Somewhat, Month is a valid function, however Month returns an Integer data type, where you are trying to compare to a Text data type.

So for example
select val FROM obs WHERE ... AND MONTH(obsdate) = '01' is not valid because '01' is text

select val FROM obs WHERE ... AND MONTH(obsdate) = 1 is valid. because month returns the integer value 1-12 for the current month.