sql server 2008 r2 - Convert nvarchar to date in query -
i have date value in string below how can query against date. how need query evtdate nvarchar , has date value 2013-07-18 06:30:30.843000000
declare @yesterday datetime - dateadd(day,diff(day,1,getdate(),0) select count(*) tbl1 evtdate >= dateadd(hour,10,@yesterday)
or
select count(*) tbl1 evtdate >- dateadd(day,'20010102',getdate()-2),'2001-01-01t10:00:00')
i assume in question want count rows in 24-hour period, starting 10 yesterday, (but not including) 10 today.
declare @yesterday datetime = dateadd(day, datediff(day,1,getdate()),0); select count(*) dbo.tbl1 convert(datetime, evtdate) >= dateadd(hour, 10, @yesterday) , convert(datetime, evtdate) < dateadd(hour, 34, @yesterday);
i recommend change evtdate
nvarchar
datetime
(and consider indexing if query pattern common). column has absolutely no business whatsoever being nvarchar
- varchar
bad enough, nvarchar
not losing built-in validation , date/time methods, you're storing 2 bytes every character, though datetime
never have characters require unicode. , how prevent storing 'floob'
or 'jimmy hoffa'
in column?
Comments
Post a Comment