mysql日期转换错误

时间:2015-09-03 14:09:10

标签: mysql sql

我有以下查询,我通过ODBC连接在MySQL中运行。

Select
PUB.oa_nltrans.company,
PUB.oa_nltrans.costcentre As Code,
PUB.oa_nltrans.docdate,

From
PUB.oa_nltrans 

Where
PUB.oa_nltrans.company = 01 And
(PUB.oa_nltrans.costcentre >= '14-01' And
PUB.oa_nltrans.costcentre <= '14-06') And
PUB.oa_nltrans.docdate = '31/08/2015' 

我正在尝试将日期限制为1天 - 2015年8月31日,但我收到无效的日期错误。

我得到的错误是:

enter image description here

有人可以建议在我的查询中使用转换的方法来做到这一点吗?

非常感谢,

2 个答案:

答案 0 :(得分:0)

select语句中的额外,

SELECT PUB.oa_nltrans.company, PUB.oa_nltrans.costcentre As Code, PUB.oa_nltrans.docdate
FROM PUB.oa_nltrans 
WHERE PUB.oa_nltrans.company = 01 
AND PUB.oa_nltrans.costcentre BETWEEN '14-01' AND '14-06'
AND date_format(PUB.oa_nltrans.docdate, '%d/%m/%Y') = '31/08/2015' 

有点整洁,因为当你选择的唯一一个表时,你不需要指定表。

SELECT company, costcentre AS Code, docdate
FROM PUB.oa_nltrans 
WHERE company = 01 
AND costcentre BETWEEN '14-01' AND '14-06'
AND date_format(docdate, '%d/%m/%Y') = '31/08/2015' 

答案 1 :(得分:0)

假设docdate是表格中的DATE字段,并且从您收到的错误中看来,您在语句中暗示的日期格式似乎是错误的格式。

尝试使用MySQL中DATE字段的默认日期格式,即YYYY-MM-DD。所以你的查询应该是:

Select
PUB.oa_nltrans.company,
PUB.oa_nltrans.costcentre As Code,
PUB.oa_nltrans.docdate,

From
PUB.oa_nltrans 

Where
PUB.oa_nltrans.company = 01 And
(PUB.oa_nltrans.costcentre >= '14-01' And
PUB.oa_nltrans.costcentre <= '14-06') And
PUB.oa_nltrans.docdate = '2015-08-31'