简体   繁体   English

如何转换 SQL 中的日期格式?

[英]How can I convert a date format in SQL?

Some recently helped me to find a date with the command shown below, it returns the date in YYYY-MM-DD format, how can I convert that to YYYYMMDD so that I can use it to filter records in my where clause?最近有人帮我用下面显示的命令查找日期,它以 YYYY-MM-DD 格式返回日期,我如何将其转换为 YYYYMMDD 以便我可以使用它来过滤 where 子句中的记录?

'''DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 MONTH''' '''DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 个月'''

You can use:您可以使用:

convert(varchar,[date_you_want_to_convert],112)

As shown in microsoft sql docs微软 sql 文档中所示

Bro I think you already have the correct format to use it into a query to filter records, see like I do a query using your date format.兄弟,我认为您已经拥有正确的格式,可以将其用于过滤记录的查询,就像我使用您的日期格式进行查询一样。

在此处输入图像描述

This is my query这是我的查询

SELECT * FROM program_authorization_data WHERE dateCreate between '2022-01-13' AND '2022-01-15';

You should tag with db2-400 when you ask questions about SQL running on OS400/IBMi/...当您询问有关在 OS400/IBMi/...上运行的 SQL 的问题时,您应该标记 db2-400

If you use a recent version of the OS you can get what you want with如果你使用最新版本的操作系统,你可以得到你想要的

dec(your_date)

And with a recent version of the OS with some recent TR installed you can get first day of preceding month as a decimal using:并且使用安装了一些最新 TR 的最新版本的操作系统,您可以使用以下方法获得上个月的第一天作为小数:

dec(current date - (day(current date) - 1) day - 1 month)

or或者

date(trunc_timestamp(current date, 'MM') - 1 month)

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

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