Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 日期\u Trunc功能未按预期工作_Sql_Amazon Web Services_Amazon Redshift - Fatal编程技术网

Sql 日期\u Trunc功能未按预期工作

Sql 日期\u Trunc功能未按预期工作,sql,amazon-web-services,amazon-redshift,Sql,Amazon Web Services,Amazon Redshift,我试图在SQL语句中使用Date_Trunc for MONTH函数,但不知何故它对我不起作用。我正在尝试提取2019年4月1日之后发生的条目。红移数据库中的原始日期格式是我尝试将其分组为月/年桶的格式:2019-04-08T00:13:20.000Z 输入 输出 "ERROR: function date_trunc("unknown", integer) does not exist Hint: No function matches the given name and argument

我试图在SQL语句中使用Date_Trunc for MONTH函数,但不知何故它对我不起作用。我正在尝试提取2019年4月1日之后发生的条目。红移数据库中的原始日期格式是我尝试将其分组为月/年桶的格式:2019-04-08T00:13:20.000Z

输入

输出

"ERROR: function date_trunc("unknown", integer) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts."

我做错了什么?

假设您使用的是Postgres,您需要在日期常量周围加引号,并且可以转换为正确的类型:

WHERE job_date >= DATE_TRUNC('month'::text, '2019-04-01'::date) 

是一个dbfiddle。

假设您使用的是Postgres,您需要在日期常量周围加引号,并且可以转换为正确的类型:

WHERE job_date >= DATE_TRUNC('month'::text, '2019-04-01'::date) 

是一个数据库文件。

函数的语法是
DATE\u TRUNC('datepart',timestamp)
,似乎您需要将其用作
DATE\u TRUNC('month',session\u utc)
(这已经截断到了
2019年4月的第一个日期,即
2019-04-01

函数的语法是
DATE\u TRUNC('datepart',时间戳)
,似乎您需要使用as
DATE\u TRUNC('month',session\u utc)
(这已经截断到
2019年4月的第一个日期,即
2019-04-01

将时间戳作为输入,并提供时间戳作为输出:

DATE_TRUNC('datepart', timestamp)
例如:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)

2019-05-01 00:00:00
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp
因此,您的行应该是:

WHERE job_date >= DATE_TRUNC('month', '2019-04-01'::timestamp) 
如果希望将输出作为日期,请附加
::date

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)::date

2019-05-01
另外,请注意,日期转换为午夜时的时间戳。这可能会导致某些比较出现差异。例如:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)

2019-05-01 00:00:00
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp
将评估为
True
,因为它与一天开始时的午夜相比较。这与比较两个日期(没有时间戳)不同。

将时间戳作为输入,并提供时间戳作为输出:

DATE_TRUNC('datepart', timestamp)
例如:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)

2019-05-01 00:00:00
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp
因此,您的行应该是:

WHERE job_date >= DATE_TRUNC('month', '2019-04-01'::timestamp) 
如果希望将输出作为日期,请附加
::date

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)::date

2019-05-01
另外,请注意,日期转换为午夜时的时间戳。这可能会导致某些比较出现差异。例如:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)

2019-05-01 00:00:00
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp

将评估为
True
,因为它与一天开始时的午夜相比较。这不同于比较两个日期(没有时间戳)。

也不起作用。错误:函数日期(“未知”、“未知”)不是唯一的提示,直到错误:无效数字,值“-”,位置4,类型:十进制代码:1207上下文:2019-04也不工作。错误::函数日期(“未知”、“未知”)不是唯一的提示,直到错误:无效数字,值“-”,位置4,类型:十进制代码:1207上下文:2019-04“datepart”是实际值还是我需要填充此变量?@Tina对不起,我忘记了文本。应该是
month
datepart'实际值还是我需要填充这个变量?@Tina对不起,我忘了那里的文字。应该是
月份