简体   繁体   English

在 SQL 中使用修剪

[英]Using Trim in SQL

SELECT
    EVENTDATE

Is returning the date 2023-04-11 00:00:00.000 -0400.返回日期 2023-04-11 00:00:00.000 -0400。 I want to trim it to only return "2023-04-11", but am struggling to understand how to use the trim function effectively.我想修剪它只返回“2023-04-11”,但我很难理解如何有效地使用修剪 function。 I am using substring because it is otherwise returning as a date format and I need it as a string.我正在使用 substring 因为它会以日期格式返回,我需要它作为字符串。 Only have a beginners educational background so appreciate any and all help.只有初学者的教育背景,所以感谢任何和所有的帮助。 The database I am using is snowflake我使用的数据库是雪花

To convert a date to string and trim in Snowflake, you could use the to_char function.要将日期转换为字符串并在 Snowflake 中修剪,您可以使用 to_char function。 Also showing date_trunc option if you want to keep as a date.如果您想保留日期,还显示 date_trunc 选项。

select eventdate, 
  to_char(eventdate, 'yyyy-mm-dd') as ed2, 
  date_trunc('DAY', eventdate::date) as ed3 --cast as date to remove time

Output: Output:

eventdate活动日期 ed2 ed2 ed3 ed3
2019-09-10 01:20:25.694 +0000 2019-09-10 01:20:25.694 +0000 2019-09-10 2019-09-10 2019-09-10 2019-09-10

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

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