我正在尝试比较两个日期时间,但它在动态查询中不起作用。为什么部分时间不起作用?我的代码是这些
ALTER PROCEDURE [dbo].[testdate]
@startDate datetime = null ,
@endDate datetime = null,
@TableName nvarchar(100) ,
@SearchString NVARCHAR(200)
AS
BEGIN
SET NOCOUNT ON;
declare @sqlcmd nvarchar(max)
set @sqlcmd= 'select * from '+@TableName +' where '
if @startDate is not null and @endDate is not null
begin
/*set @sqlcmd=@sqlcmd + ' ( DateLog between'''
+ convert (nvarchar(50), @startDate, 101)
+''' and '''
--+ convert (nvarchar(50),DATEADD(D,1, @endDate), 101)
+ convert (nvarchar(50), @endDate, 101)
+ ''' ) and '
*/
set @sqlcmd=@sqlcmd + ' ( DateLog between @startDate and @endDate ) and '
end
set @sqlcmd=@sqlcmd + '('
SELECT
@sqlcmd = @sqlcmd + ' [' + SYSCOLUMNS.NAME + '] LIKE N''%'+@SearchString+'%'' or '
FROM SYSCOLUMNS
WHERE OBJECT_NAME(id) = @TableName AND TYPE_NAME(SYSCOLUMNS.XTYPE) IN ('VARCHAR','NVARCHAR','CHAR','NCHAR','INT','DECIMAL')
set @sqlcmd = @sqlcmd + '1<>1)'
--print @sqlcmd
Exec(@sqlcmd)
END
我的表 AccountLog 有这些字段和数据
UserName DateLog
Salah 2012-04-06 22:06:19.493
John 2012-08-06 22:06:22.800
这个查询是真的
select * from AccountLog where DateLog between '2012-04-06T22:06:19' and '2012-08-06T22:06:23'
但是这个查询没有给我适当的结果
declare @a datetime
declare @b datetime
set @a = '2012-04-06T22:06:19'
set @b = '2012-08-06T22:06:23'
exec testdate @a,@b,'AccountLog',N'test'