Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function to_char(unknown, unknown) is not unique

Tags:

postgresql

When running the following the query.select * from surgicals where to_char(dt_surgery ,'DD-MM-YYYY' ) = to_char('12-02-2012','DD-MM-YYYY');

the error coming as 'SQL state 42725: ERROR: function to_char(unknown, unknown) is not unique'

How to run above select query?

like image 598
alexkd Avatar asked Jun 27 '12 11:06

alexkd


3 Answers

You probably mean to_char('12-02-2012'::date, 'DD-MM-YYYY'). to_char cannot convert a plain string to string. Still, it does not seem to make sense, you need one of these two, depending on the format of your date constant (which cannot be determined from the actual example date you provided):

select * from surgicals where to_char(dt_surgery ,'DD-MM-YYYY' ) = '12-02-2012';
select * from surgicals where to_char(dt_surgery ,'MM-DD-YYYY' ) = '12-02-2012';
like image 154
lanzz Avatar answered Nov 14 '22 19:11

lanzz


The wrongness here is that you're doing string comparison of dates. Use date/time math, which can take into account fun things like time zones etc. and still get it right.

like image 31
Scott Marlowe Avatar answered Nov 14 '22 18:11

Scott Marlowe


Maybe this is what you need:

SELECT *
FROM surgicals 
WHERE date_trunc('day', dt_surgery) = '2012-02-12'
    ;
like image 30
wildplasser Avatar answered Nov 14 '22 18:11

wildplasser