Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

converting text to datetime

hi experts,
 
I the following string in a sql select query:

Convert(varchar(12),DeadlineDate,107)+" "+Convert(varchar(5),DeadlineTime, 108) Deadline

this is merging a date and time from the two datetime fields to provide a single formatted datetime on a web page.

I am being asked to order on this end datetime (Deadline) which is causing me problems since it is now text. Can I convert the result back to a datetime somehow?
ASKER CERTIFIED SOLUTION
Avatar of Steve Wales
Steve Wales
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of forsters

ASKER

actually my colleague has just solved this for me with the following in the order by:

DATEADD(day, 0, DATEDIFF(day, 0, deadlinedate)) + DATEADD(day, 0 - DATEDIFF(day, 0, deadlinetime), deadlinetime).

many thanks for your reply I had also tried to resolve in the way you suggest but deadlinetime is actually storing a date ( even though it isn't required) and that date was throwing the ordering out on occasions where it conflicted with the deadlinedate.

I will give points regardless - apreciated
?? maybe just cast the time field to time without date

order by DeadlineDate, cast(DeadlineTime as time)
ha, very good yes that would probably do it, i'll have a go later...