我使用以下存储过程,它将datetime变量作为输入
ALTER PROCEDURE dbo.report @createfrom as datetime=null,@createto as datetime=null
AS
BEGIN
declare @sql varchar(1000)
declare @s datetime;
declare @s1 datetime;
--set @createfrom='7/7/2014 12:00:00 AM'(my input is like in this format)
set @sql = N'select * from table1 where createdon >=''' + cast(@createfrom as datetime) + '''and createdon <=''' + cast(@createto as datetime) + ''' AND createdon is not null'
exec(@sql)
END
我不知道我做错了什么。 createdon
是table1中的datetime
变量。我在代码本身中给出了输入参数格式。我想坚持cast
而不是convert
答案 0 :(得分:0)
我知道你说你想坚持转换,但最好将日期格式化为字符串中的yyyymmdd
e.g。
ALTER PROCEDURE dbo.report @createfrom as datetime=null,@createto as datetime=null
AS
BEGIN
declare @sql varchar(1000)
declare @s datetime;
declare @s1 datetime;
--set @createfrom='7/7/2014 12:00:00 AM'(my input is like in this format)
set @sql = N'select * from table1 where createdon >=''' + CONVERT(VARCHAR(8), @createfrom, 112) + '''and createdon <=''' + CONVERT(VARCHAR(8), @createto, 112) + ''' AND createdon is not null'
exec(@sql)
END