sql server - Converting a string to a date time in SQL -
i'm importing data different system , datetime stored string in format:
20061105084755es
yyyymmddhhmmss(es/ed)
es
est , ed
edt.
i have query table last 30 days. i'm using conversion query:
select convert( datetime, left(cdts, 4)+'-'+substring(cdts, 5,2)+'-'substring(cdts, 7,2)+' '+substring(cdts, 9,2) +':'+substring(cdts, 11,2)+':'+substring(cdts, 13,2) dt tb1 dt < getdate()-30
i'm looking more efficient query reduce time taken. table has around 90 million records , query runs forever.
no calculation @ runtime going speed query if performing calculation , need filter against result of calculation - sql server forced perform table scan. main problem you've chosen store dates string. variety of reasons, terrible decision. string column indexed @ least? if so, may data last 30 days:
declare @thirtydays char(8); set @thirtydays = convert(char(8),dateadd(day,datediff(day,0,getdate()),0)-30,112); select ... cdts >= @thirtydays;
if need return data of history except past 30 days, isn't going either, because unless pulling data indexed column, efficient approach retrieving of data in table use clustered index scan. (if retrieving narrow set of columns, may opt index scan, if have covering index.) so, bottleneck in of these scenarios not formula can fix, rather time takes retrieve large volume of data, transmit on network, , render on client.
also, aside, can't this:
select + b c dbo.somewhere c > 10;
c
doesn't exist in dbo.somewhere
, expression derived in select
list. select
list parsed second last (right before order by
), can't reference in where
clause doesn't exist yet. typical workarounds repeat expression or use subquery / cte.
Comments
Post a Comment