简体   繁体   English

Date_Trnc 和 To_Date 问题 SQL

[英]Date_Trunc and To_Date Questions SQL

  1. Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to eg the start of the week?我们可以将 date_trunc 用于我们试图“截断”(不确定该术语是否可以在此处应用)的日期(不是日期时间)到例如一周的开始? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)?因此,如果我有date_trunc(week, 28/10/2020)并且我想获得 10 月 28 日所在的那一周的开始(即 10 月 26 日)? I tried this in my SQL command line but I get error messages.我在 SQL 命令行中尝试过此操作,但收到错误消息。

  2. If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY');如果我正在做: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)?如何确保生成的格式是我指定的日期格式(而不是默认日期格式)? For example if I want it in format DD-MM-YYYY?例如,如果我想要 DD-MM-YYYY 格式?

  3. select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? select to_char(date '2017-06-02', 'MM') < 在这个例子中,为什么我们需要“date”才能工作? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). to_char 的一般格式应该是 TO_CHAR (timestamp_expression, 'format')。 I don't see in this general format that we need "day".在这种通用格式中,我没有看到我们需要“天”。

  4. if I have a WHERE filter like to_char(order_date, '20-10-2020') , and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char).如果我有一个像to_char(order_date, '20-10-2020')这样的 WHERE 过滤器,并且确实有一些具有此订单日期的行,如果这些行在 DATE 中,这些行是否仍会显示在我的结果中(运行查询后)格式(所以 20 Oct 是日期格式)而不是字符串(这是我在做 to_char 时过滤的内容)。 I know there would be no need to use to_char in this case but just asking..我知道在这种情况下不需要使用 to_char 而只是问..

  1. yes, you can use date in text form but you have to cast it to a correct type是的,您可以以文本形式使用日期,但您必须将其转换为正确的类型

these queries will work这些查询将起作用

select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as 
select date_trunc('week', '2020-10-28'::datetime);

and return timestamp 2020-10-26 00:00:00.000000并返回时间戳2020-10-26 00:00:00.000000

note, next query注意,下一个查询

select date_trunc('week', '28/10/2020'::date);

will fail with error date/time field value out of range: "28/10/2020";将失败,错误date/time field value out of range: "28/10/2020";

  1. You can use to_char , it returns text , if you need a date format you have to case it again您可以使用to_char ,它返回text ,如果您需要日期格式,则必须再次区分大小写
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;

select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
  1. '2017-06-02' is a text and it can't be automatically converted to timestamp. '2017-06-02' 是文本,不能自动转换为时间戳。 Actually I don't know a text format which can.其实我不知道可以的文本格式。

  2. No, you need to explicitly cast into date type to use it as a filter不,您需要显式转换为date类型才能将其用作过滤器

where order_date = date_stored_as_a_text::date

I am answering the questions in a different order as there are some wrong assumptions:我以不同的顺序回答问题,因为有一些错误的假设:

Question 3问题 3

There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works. '2017-06-02'date '2017-06-02'之间有一个普遍的区别 - 第一个只是一个 varchar,一个字符串,不被 Redshift 处理为日期,第二个告诉 Redshift 处理字符串作为日期,因此有效。

Question 2问题2

A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift.日期数据类型列没有格式 - 您可以使用可以以不同格式显示日期列的 sql 客户端,但是,这不是 redshift 的功能。 SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.告诉 redshift 将字符串'02 Oct 2001'转换为日期。

Question 1问题 1

DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function ( Also shown in the example of AWS ). DATE_TRUNC('datepart', timestamp)也支持周作为日期部分- 请参阅日期或时间戳函数的日期部分也显示在 AWS 的示例中)。 You should also be able to provide a date instead of a timestamp.您还应该能够提供日期而不是时间戳。

Question 4问题 4

to_char(order_date, '20-10-2020') is not a filter and you are using it wrong. to_char(order_date, '20-10-2020')不是过滤器,你用错了。

AWS TO_CHAR AWS TO_CHAR

TO_CHAR converts a timestamp or numeric expression to a character-string data format. TO_CHAR 将时间戳或数字表达式转换为字符串数据格式。

I guess you are rather looking for:我猜你是在寻找:

where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM