根据日期选择多个记录,但是我在返回的日期不在任何想法的范围内?
SELECT *
FROM TABLE
WHERE (
PAYMENT_DATE >= CONVERT(DATETIME, '2010-05-06')
AND PAYMENT_DATE <= CONVERT(DATETIME, '2013-11-11')
)
例如,这将返回日期为“2008-04-10 00:00:00.000”的记录
答案 0 :(得分:2)
如果您不使用参数,请始终使用不变的日期时间格式:
SELECT *
FROM [Table]
WHERE ([PaymentDate] >= '20100506' AND [PaymentDate] <= '20131111')
答案 1 :(得分:1)
在Clause之间使用SQL检查范围是很好的。
SELECT *
FROM table
WHERE PAYMENT_DATE BETWEEN CONVERT(datetime, '2010-05-06')
AND CONVERT(datetime, '2013-11-11')
答案 2 :(得分:0)
试试这个
这必须给出预期的结果
SELECT *
FROM table
WHERE (Convert(datetime,PAYMENT_DATE,103) >= CONVERT(datetime, '2010-05-06')
AND Convert(datetime,PAYMENT_DATE,103) <= CONVERT(datetime, '2013-11-11'))
答案 3 :(得分:0)
此外,您可以按名称或缩写指定月份,如下所示:
SELECT *
FROM TABLE
WHERE (
PAYMENT_DATE >= CONVERT(DATETIME, '2010-May-06')
AND PAYMENT_DATE <= CONVERT(DATETIME, '2013-Nov-11')
)
或者您可以使用BETWEEN
子句来简化查询:
SELECT *
FROM TABLE
WHERE
PAYMENT_DATE BETWEEN '2010-May-06' AND '2013-Nov-11'