Solved

converting text to datetime

Posted on 2014-04-11
4
183 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 22

Accepted Solution

by:
Steve Wales earned 500 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 48

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
View SQL database for NPS accounting 4 19
Removing SCCM 2016 4 51
how to resize column length with primary ket 4 34
Access PS SQLSERVER from powershell 1 30
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

739 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