Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting text to datetime

Posted on 2014-04-11
4
Medium Priority
?
186 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 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 39993949
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
ID: 39994013
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 49

Expert Comment

by:PortletPaul
ID: 39995463
?? maybe just cast the time field to time without date

order by DeadlineDate, cast(DeadlineTime as time)
0
 

Author Comment

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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

916 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