0

I'm importing data from a different system and the datetime is stored as string in this format:

20061105084755ES

yyyymmddhhmmss(es/ed) where es is EST and ed is EDT.

I will have to query this table for the last 30 days. I'm using the 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)
as dt
from tb1
where dt < getdate()-30

I'm looking for a more efficient query that will reduce the time taken. This table has around 90 million records and the query runs forever.

1
  • 2
    You might consider removing that picture and actually pasting the query text in. For some of us (am I getting old?) that picture is really really hard to read. Also, we generally like to see the whole query and, if available, the execution plan. The first question people will ask themselves is whether that conversion is the issue or something else -- like a join between tables without using indexes. Commented Aug 23, 2013 at 17:30

3 Answers 3

2

No calculation at runtime is going to speed this query up if you are performing the calculation and then need to filter against the result of the calculation - SQL Server will be forced to perform a table scan. The main problem is that you've chosen to store your dates as a string. For a variety of reasons, this is a terrible decision. Is the string column indexed at least? If so, then this may help get the data only from the last 30 days:

DECLARE @ThirtyDays CHAR(8);
SET @ThirtyDays = CONVERT(CHAR(8),DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()),0)-30,112);

SELECT ...
WHERE cdts >= @ThirtyDays;

If you need to return all the data from all of history except the past 30 days, this isn't going to help either, because unless you are only pulling data from the indexed column, the most efficient approach for retrieving most of the data in the table is to use a clustered index scan. (If you are retrieving a narrow set of columns, it may opt for an index scan, if you have a covering index.) So, your bottleneck in much of these scenarios is not something a formula can fix, but rather the time it takes to actually retrieve a large volume of data, transmit it over the network, and render it on the client.

Also, as an aside, you can't do this:

SELECT a + b AS c FROM dbo.somewhere
WHERE c > 10;

c doesn't exist in dbo.somewhere, it is an expression derived in the SELECT list. The SELECT list is parsed second last (right before ORDER BY), so you can't reference something in the WHERE clause that doesn't exist yet. Typical workarounds are to repeat the expression or use a subquery / CTE.

Sign up to request clarification or add additional context in comments.

Comments

1

One potential option is to add a date column to your table and populate that information on load. This way the conversion is all done before you need to query for it.

Then, make sure you have an index on that field which the actual query can take advantage of.

1 Comment

thanks for the response. I have rights only the query the tables.
0
convert(datetime,stuff(stuff(stuff(datevalue, 9, 0, ' '), 12, 0, ':'), 15, 0, ':'))

or

    Convert(time,Dateadd(SECOND,
        Right(DateValue,2)/1,
        Dateadd(MINUTE,
                Right(DateValue,4)/100,
                Dateadd(hour,
                        Right(DateValue,6)/10000,
                        '1900-01-01')))) +
convert(datetime,LEFT(datevalue,8))

Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.