postgresql 函数 to_char(unknown, unknown) 不是唯一的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11225011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:02:52  来源:igfitidea点击:

function to_char(unknown, unknown) is not unique

postgresql

提问by alexkd

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');

运行以下查询时。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'

错误为“SQL 状态 42725:错误:函数 to_char(unknown, unknown) 不是唯一的”

How to run above select query?

如何在选择查询上方运行?

采纳答案by lanzz

You probably mean to_char('12-02-2012'::date, 'DD-MM-YYYY'). to_charcannot 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):

你可能是说to_char('12-02-2012'::date, 'DD-MM-YYYY')to_char无法将普通字符串转换为字符串。尽管如此,这似乎没有意义,您需要这两者之一,具体取决于日期常量的格式(无法从您提供的实际示例日期确定):

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';

回答by Scott Marlowe

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.

这里的错误在于您正在对日期进行字符串比较。使用日期/时间数学,它可以考虑时区等有趣的事情,并且仍然正确。

回答by wildplasser

Maybe this is what you need:

也许这就是你需要的:

SELECT *
FROM surgicals 
WHERE date_trunc('day', dt_surgery) = '2012-02-12'
    ;