简体   繁体   English

SQL查询不返回日期范围之间的数据

[英]SQL Query not returning data between date range

I am trying to return results between column "Date of Payment" and when I run the query below I am not getting the correct results. 我试图返回“付款日期”列之间的结果,而当我在下面运行查询时,我没有得到正确的结果。 I get data from that column where it could be 10/1/2013..Any idea what I might be doing wrong here? 我从那列可能是2013年10月1日的数据中获取数据。任何想法我在这里可能做错了吗? I just want the rows for 2017.. TIA 我只想要2017年的行。.TIA

Date of payment column type is a nvarchar(50) 付款日期列类型为nvarchar(50)

Sample column data is like 10/31/2017... 样本列数据类似于2017年10月31日...

SELECT     ID,  [Date of Payment]
FROM         tblData
WHERE     ([Date of Payment] BETWEEN '1/1/2017' AND '12/31/2017')

Try next code And avoid put spaces in field name and replace it with underscore 尝试下一个代码,并避免在字段名称中放置空格,并用下划线替换

SELECT     ID,  PaymentDate
FROM         tblData
WHERE (PaymentDate BETWEEN CONVERT(DATETIME, '2017-01-01', 102) AND CONVERT(DATETIME, '2017-12-30', 102))

(I am assuming SQL Server based on the syntax.) (我假设基于语法的SQL Server。)

Fix the [Date of Payment] data in your table. 修正表格中的[Date of Payment]数据。

update tblData
    set [Date of Payment] = convert(date, [Date of Payment], 101);

alter tblData
    alter column [Date of Payment] date;

Voila! 瞧! Your problem is fixed! 您的问题已解决!

You should also change the data type to use ISO standard formats: 您还应该更改数据类型以使用ISO标准格式:

WHERE [Date of Payment] BETWEEN '2017-01-01' AND '2017-12-31'

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

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