Timestamp stored as Varchar, query range

I have a SQL server table written to by ForceTen LMS; I did not create the table and cannot modify it or the code that writes to it.
In it are stored some timestamps. However, they are stored as Varchar in this format:
Starttime               EndTime
2005-10-12-21.40.37	    2005-10-19-16.44.05
2005-10-12-21.40.57	    2005-10-21-12.48.35

Open in new window

How can I query a range for the EndTime?
SELECT * FROM Table WHERE EndTime >= :param1 AND EndTime < :param2

Open in new window

LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Try this:
SELECT  *
FROM    Table
WHERE   CONVERT(datetime, REPLACE(STUFF(StartTime, 11, 1, ' '), '.', ':')) >= :param1
AND     CONVERT(datetime, REPLACE(STUFF(EndTime, 11, 1, ' '), '.', ':')) < :param2

Open in new window

Explanation: SQL likes a datetime format of yyyy-mm-dd hh:mm:ss.

The STUFF command "stuffs" a space in position 11 where the hypen (-) is.
The REPLACE command "replaces" those periods with colons.

At that point the fields can be CONVERTed to datetime types.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerAuthor Commented:
Perfect...I wish they had an easier way to do this, like in MySQL...
0
PortletPaulfreelancerCommented:
>>" SQL likes a datetime format of yyyy-mm-dd hh:mm:ss."

If you specify the style number 120 then you can depend on yyyy-mm-dd hh:mm:ss being used

SELECT  *
FROM    Table
WHERE   CONVERT(datetime, REPLACE(STUFF(StartTime, 11, 1, ' '), '.', ':') ,120 ) >= :param1
AND     CONVERT(datetime, REPLACE(STUFF(EndTime, 11, 1, ' '), '.', ':') ,120 ) < :param2

SQL Server Date Styles (formats) using CONVERT()
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.