Solved

converting text to datetime

Posted on 2014-04-11
4
178 Views
Last Modified: 2014-04-14
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?
0
Comment
Question by:forsters
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
Comment Utility
Is the order by happening at the same time as the query ?  Or after you've done the convert ?

If it's part of the same query you can do:

select ....
, Convert(varchar(12),DeadlineDate,107)+" "+Convert(varchar(5),DeadlineTime, 108) Deadline
, ....
from yourtable
order by DeadlineDate, DeadlineTime
0
 

Author Comment

by:forsters
Comment Utility
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
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
?? maybe just cast the time field to time without date

order by DeadlineDate, cast(DeadlineTime as time)
0
 

Author Comment

by:forsters
Comment Utility
ha, very good yes that would probably do it, i'll have a go later...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now