显示同一天的SQL数据

时间:2015-06-18 15:48:49

标签: sql-server-2008

我需要能够显示搜索参数显示当天的信息。我认为这个查询会削减芥末,但它会出现错误

Msg 241, Level 16, State 1, Line 13
     

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

这是我的语法,我该怎么做才能删除错误?

DECLARE
  @startDate datetime,
  @endDate datetime

SET @startDate = '06/11/2015'
SET @endDate = '06/11/2015'

Set @startDate = convert(varchar(100),@startDate, 101) + ' 00:00:00 AM' 
Set @endDate = convert(varchar(100),@endDate, 101) + ' 23:59:59 PM'

select 'Artay' As [Bank Name]
,COUNT(Transaction) As LC
from dbo.financialtransactions
where cleardate between ''' + @startDate + ''' and ''' + @endDate + '''

1 个答案:

答案 0 :(得分:1)

将最后一行更改为

where cleardate between @startDate and @endDate

您的整个脚本应如下所示,Transaction是一个保留字,因此请务必使用[]

进行转义
DECLARE
  @startDate datetime,
  @endDate datetime

SET @startDate = '06/11/2015'
SET @endDate = '06/11/2015'

Set @startDate = convert(varchar(100),@startDate, 101) + ' 00:00:00 AM' 
Set @endDate = convert(varchar(100),@endDate, 101) + ' 23:59:59 PM'

select 'Artay' As [Bank Name]
,COUNT([Transaction]) As LC
from dbo.financialtransactions
where cleardate between @startDate and @endDate