这个SQL查询有什么问题。我在转换
附近遇到语法错误"SELECT * FROM collections c where c.submittedBy='679' AND CONVERT(varchar, c.eventTime, 103) ='2017-06-21'";
eventTime是" eventTime":" 2017-06-21T12:20:03.9366135 + 05:30"
错误是
{"Message: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":64,\"end\":71},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near 'CONVERT'.\"}]}\r\nActivityId: a0c51c53-fca0-4c81-8f10-65348749e3d2"}
答案 0 :(得分:4)
编写此查询(跨数据库)的最佳方法是:
SELECT c.*
FROM collections c
WHERE c.submittedBy='679' AND
c.eventTime >= '2017-06-21' AND
c.eventTime < '2017-06-22';
这允许数据库使用适当的索引进行过滤。请注意,如果submittedBy
是数字,则不要使用单引号。
答案 1 :(得分:1)
转换为日期之前转换为varchar
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(varchar(10), CONVERT(date, c.eventTime, 103)) = '2017-06-21'
如果eventTime已经是datetime字段,那么也应该这样..
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(date, c.eventTime) = '2017-06-21'