Solved

convert char to datetime in sql

Posted on 2006-11-17
13
1,354 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
Comment Utility
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
Comment Utility
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
Comment Utility
but,
error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
thanks,
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
What DBMS?
0
 

Author Comment

by:eclamshell
Comment Utility
sql server 2005
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

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

Author Comment

by:eclamshell
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now