1

我对 sql-server 比较陌生;我正在尝试从表单变量中提取开始日期和结束日期作为字符串,然后将其转换为日期时间(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

我不断收到的错误是:

从字符串转换日期时间时转换失败。

我该如何解决?

4

2 回答 2

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
于 2012-10-12T15:10:41.657 回答
0

你不必强制转换

declare @startdate varchar(50)
declare @enddate varchar(50)
declare @start datetime = @startdate, @end datetime = @enddate
select @start, @end
于 2016-03-15T15:46:18.023 回答