[英]How can I convert date format from YYYY-MM-DD to YYYY-MM in sql
I have a specific date in the format of YYYY-MM-DD but want to convert it to YYYY-MM format in a table.我有一个 YYYY-MM-DD 格式的特定日期,但想在表格中将其转换为 YYYY-MM 格式。 I want this change to apply to the full table.
我希望此更改适用于整个表格。 can anyone help me out, please?
任何人都可以帮我吗? Thank you.
谢谢你。
Assuming that you have a date
datatype or the-like ( datetime
, timestamp
), you can use date_format()
to represent your date in the target format:假设您有
date
数据类型或类似数据( datetime
、 timestamp
),您可以使用date_format()
以目标格式表示您的日期:
date_format(mydate, '%Y-%m')
This returns a string in the target format.这将返回目标格式的字符串。 It does not make sense to convert your
date
column to a string though.但是,将
date
列转换为字符串没有意义。 Keep that column as it is, and maybe use a computed column to automatically derived the string representation you want:保持该列不变,并可能使用计算列自动派生您想要的字符串表示形式:
create table mytable (
...
mydate date,
mynewcol varchar(7) as (date_format(mydate, '%Y-%m'))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.