我对sql-server比较新;我试图从一个表单变量拉出一个开始日期和结束日期作为字符串,然后将其转换为datetime(yyyy-mm-dd)格式,我似乎无法找到任何有用的东西。尝试的代码和结果错误如下。任何建议将不胜感激。
declare @startdate as varchar
declare @enddate as varchar
set @startdate=cast(@startdate as datetime)
set @enddate=cast(@enddate as datetime)
SELECT order_date, inv_no
from invoices
where order_date between @startdate and @enddate
我不断得到的错误是:
从字符串转换日期时转换失败。
我该如何解决这个问题?
答案 0 :(得分:1)
指定varchar
的长度:
declare @startdate as varchar(10)
declare @enddate as varchar(10)
set @startdate=cast(@startdate as datetime)
set @enddate=cast(@enddate as datetime)
SELECT order_date, inv_no
from invoices
where order_date between @startdate and @enddate
答案 1 :(得分:0)
你不必必须施展
declare @startdate varchar(50)
declare @enddate varchar(50)
declare @start datetime = @startdate, @end datetime = @enddate
select @start, @end