Solved

convert char to datetime in sql

Posted on 2006-11-17
13
1,366 Views
Last Modified: 2008-02-07
Hello,
Due to the existing DB design, I have a column called begintime, the format is varchar(7) looks like 09:30PM and may have 9:30PM format. Another column called begindate has datetime format. I want to write a function to convert and combine these two column to a datetime format and use the result in Order By clause to compare the begindate and begintime.

thanks,

eclamshell  
0
Comment
Question by:eclamshell
13 Comments
 
LVL 1

Expert Comment

by:dacz
ID: 17967785
SELECT CONVERT(datetime, begindate , 101) + ' ' + CONVERT(datetime,begintime,108) AS BeginDateTime
FROM [table]
ORDER BY CONVERT(datetime, begindate , 101) + ' ' + CAST(begintime AS datetime)
0
 
LVL 1

Expert Comment

by:dacz
ID: 17967790
i want o modify my post:

SELECT CONVERT(datetime, begindate , 101) + ' ' + CONVERT(datetime,begintime,108) AS BeginDateTime
FROM [table]
ORDER BY CONVERT(datetime, begindate , 101) + ' ' + CONVERT(datetime,begintime,108)
0
 

Author Comment

by:eclamshell
ID: 17967911
but,
error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
thanks,
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:awking00
ID: 17967915
What DBMS?
0
 

Author Comment

by:eclamshell
ID: 17968007
sql server 2005
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17970873
select * from [table]
ORDER BY
  cast(begindate + begintime as datetime)
0
 

Author Comment

by:eclamshell
ID: 17980597
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
0
 
LVL 5

Expert Comment

by:sivachirravuri
ID: 17986918
Just a example for you...
SELECT
   GETDATE() AS UnconvertedDateTime,
   CAST(GETDATE() AS nvarchar(30)) AS UsingCast,
   CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601  ;
GO
0
 
LVL 5

Accepted Solution

by:
rk_india1 earned 500 total points
ID: 18395844
as a example I placed:

Convert Char To Datetime
--------------------------------------------------------------------------------

I have columns with the following char format:

06/01/2007 12:00


To convert character data to desired style you should convert character data to date first

convert(datetime, '06/01/2007 12:00')

Then this date could be converted to a different style

convert(varchar(20), convert(datetime, '06/01/2007 12:00'), 101)

--------------------------------------------------------------------------------

0
 

Author Comment

by:eclamshell
ID: 18839734
However, how can I handle the formate other than 09:30PM and 9:30PM. may be an error handler to handle the rest cases. thanks,
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

679 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