Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting text to datetime

Posted on 2014-04-11
4
Medium Priority
?
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

722 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