错误之间的SQL选择日期转换失败

时间:2018-07-24 04:11:18

标签: sql sql-server database sql-server-2008

我有一列数据类型为date

当我运行以下语句时:

select count(*) as dt from dbo.logs where dateLogged>= '2018-02-01' and dateLogged < ='2018-02-30';

我得到Conversion failed when converting date and/or time from character string.

4 个答案:

答案 0 :(得分:0)

select count(*) as dt from dbo.logs where CAST(dateLogged as date) >= '2018-02-01' and CAST(dateLogged as date) < ='2018-02-30'; 
OR
select count(*) as dt from dbo.logs where CAST(dateLogged as date) BETWEEN '2018-02-01' and '2018-02-30'; 

答案 1 :(得分:0)

'2018-02-30'不是有效日期。将其替换为有效日期,然后尝试以下查询:

select count(*) as dt from dbo.logs 
where cast(dateLogged as date) 
between '2018-02-01' and '2018-02-28';

答案 2 :(得分:0)

尝试:->

设置dateformat dmy

选择count(*)作为dt 来自dbo.logs 其中dateLogged> = '01 -02-2018'和dateLogged <= '30 -02-2018'

答案 3 :(得分:0)

请尝试将dateLogged列转换为具有两个选项的日期

Cast(dateLogged  as datetime)

Convert(varchar(30),'7/7/2011',102)