我从前面的开发人员那里得到了一段代码,其中保存的数据库日期如下
'Jan 1 2018 12:01PM'现在有一个用于报表输出的存储过程,该过程获取日期范围,
数据类型是
@datefrom varchar(50),
@dateto varchar(50)声明是这样的
where
(CAST(ai.date_created as DATETIME2) between @datefrom and @dateto) AND
ai.sex = (CASE WHEN @sex = 'ALL' THEN ai.sex ELSE @sex END) AND
ai.special_health_condition = (CASE WHEN @special = 'ALL' THEN ai.special_health_condition ELSE @special END)
END执行代码时,此错误显示
Conversion failed when converting date and/or time from character string我试图根据ai.date_created的答案改变this的格式,但似乎没有什么效果。这里怎么了?对不起,我是个刚毕业的学生,这对我来说简直是个谜。提前谢谢。
更新
代码是这样的:
@datefrom date,
@dateto date,
@sex varchar(50),
@special varchar(50)
AS
BEGIN
select
ai.application_no,
ai.franchise_no,
ai.operator_name,
ai.full_address,
m.motor_no,
ai.date_created
,ai.frist_name,
ai.middle_name,
ai.surname,
ai.citizenship,
ai.primary_occupation,
ai.contact_no,
ai.email_add,
ai.street,
ai.subdivision,ai.barangay,
ai.city,
ai.sex,
ai.civil_status,
ai.type,
ai.type_of_ownership,
m.chassis_no,
m.year_model,
m.plate_no,
b.brand_name,
ai.tax_certification_issued_on,
ai.tax_certification_no,
m.toda_id
,ai.age,
ai.special_health_condition
from vfTA_tblApplicationInfo as ai inner join
vfTA_tblMotor as m on m.motor_id = ai.motor_id inner join
vfTA_tblBrand as b on b.brand_id = m.brand_id
where
(CAST(ai.date_created as DATETIME2) between @datefrom and @dateto) AND
ai.sex = (CASE WHEN @sex = 'ALL' THEN ai.sex ELSE @sex END) AND
ai.special_health_condition = (CASE WHEN @special = 'ALL' THEN ai.special_health_condition ELSE @special END)
END数据显示日期为:2018年1月1日12:00 31,dateto =‘2018年1月31日11:59PM',有性=’所有‘,特别= 'All’
这是之前开发人员留下的代码。提前谢谢。
发布于 2018-02-22 00:43:46
我已经解决了这个问题
首先我改变了这个:
@datefrom date,
@dateto date对此:
@datefrom varchar(50),
@dateto varchar(50)我还改变了CAST to TRY_CAST。有关'Incorrect syntax near '1'的错误是由于数据类型日期(应该是varchar(50) )造成的。谢谢你的帮助。
https://stackoverflow.com/questions/48901736
复制相似问题