[英]Change the format of date column
I have a column called purchaseDate
of type DATE
. 我有一列名为
DATE
purchaseDate
。 I want to write a query that returns purchaseDate
in YYYY-MM-DD
format. 我想编写一个查询,以
YYYY-MM-DD
格式返回purchaseDate
。
I'm having trouble doing this while using a between function. 我在使用ween函数时很难做到这一点。 Here's my code
这是我的代码
select *
from purchaseOrders
where purchaseDate between '20-09-18' and '20-10-18'
This is the result I expect to get: 这是我期望得到的结果:
purchaseDate
------------
2018-09-20
2018-09-21
2018-09-22
2018-10-19
2018-10-20
How can I do this? 我怎样才能做到这一点?
Try using stftime() in your query: 尝试在查询中使用stftime() :
select *
from purchaseOrders
where purchaseDate between strftime(%s, '20-09-18') and strftime(%s, '20-10-18')
The syntax for the strftime function in SQLite is:
SQLite中的strftime函数的语法为:
strftime(format, timestring [, modifier1, modifier2, ... modifier_n ])
There are also some other alternatives discussed here: 这里还讨论了其他一些替代方法:
You can simply do this: 您可以简单地做到这一点:
select CONVERT(varchar(10), purchaseDate, 120) AS 'Purchase Date'
from purchaseOrders
where purchaseDate
between '20-09-18' and '20-10-18';
More here: How to get a date in YYYY-MM-DD format from a TSQL datetime field? 更多信息: 如何从TSQL日期时间字段获取YYYY-MM-DD格式的日期?
Edit 2: 编辑2:
SQLite requires date to be in YYYY-mm-dd format or other recognizable formats in this post: SQL Select between dates SQLite要求日期采用YYYY-mm-dd格式或本帖子中其他可识别的格式: SQL在日期之间选择
You can simply do this: 您可以简单地做到这一点:
select *
from purchaseOrders
where purchaseDate
between '2018-09-18' and '2018-10-18'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.