473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting varchar field data into time

19
Please Guys help me out , as I need the solution of this problem very fast.Please help me.


My table contains a varchar field which contains time like : 04:09PM

Now the problem is that I have to convert this into time.Please help me to suggest how to convert this data into time as I have to create various reports.

I have also tried to put time datatype instead of varchar.But when I inserted this value -it gives error like this:


Incorrect time value: '04:09PM' for column 'time1' at row 1


Please suggest me what should I do and How should I perform this task.


Thanking you all.
Dec 19 '07 #1
5 7291
code green
1,726 Expert 1GB
A MySql DATETIME field expects the format 'yyyy-mm-dd hh:mm:ss'.
So the times and dates inserted must follow that format.
My table contains a varchar field which contains time like : 04:09PM
A VARCHAR field contains strings not time.
Now the problem is that I have to convert this into time
Do you mean convert a VARCHAR field to a DATETIME field or convert your strings in to DATETIME values?
Dec 20 '07 #2
ITCraze
19
I mean to say that I am importing the data from txt file to MySql table.And that table contains time datatype:

and my txt file contains 04:02PM.So I have to move this data into that time field.And When I import this file to my table.I get the error:

Invalid data '4:02PM' for time .


So please help me out.,
Dec 20 '07 #3
code green
1,726 Expert 1GB
I have never used it but you could try the MySql STR_TO_DATE().
This is from MySql manual
Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE(str,format) 
  2.  
  3. This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. 
  4. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. 
  5.  
  6. The date, time, or datetime values contained in str should be given in the format indicated by format. 
  7. For the specifiers that can be used in format, see the DATE_FORMAT() function description. 
  8. If str contains an illegal date, time, or datetime value, STR_TO_DATE() returns NULL.
Dec 20 '07 #4
ITCraze
19
Thank you for replying so quickly to my answer.


I have tested the solution that you gave me but its giving me error:

SELECT STR_DATE('04:02PM','%r');

then its giving me error like :

Incorrect datetime value: '04:02PM' for function str_to_time
Dec 20 '07 #5
code green
1,726 Expert 1GB
I think it should be
Expand|Select|Wrap|Line Numbers
  1. SELECT STR_TO_DATE('04:02PM','%H:%i%p');
Dec 24 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bill | last post by:
In an effort to improve the speed of queries against my main table, I'll be indexing a column whose data type is varchar(50). Would I be better off (better performance) if I changed the column's...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
1
by: jaYPee | last post by:
I have created a stored procedure that contain this field (below) in order to meet certain criteria. But my problem is when I try to run the stored procedure I encounter an error "Error converting...
1
by: luna | last post by:
got so far then it broke and i cant get it working again - it was updating fine but not inserting and now im getting a "Error converting data type varchar to numeric" which i didnt have before.......
4
by: Dan Lewis | last post by:
I've imported a ms access database into a table in a mysql database. The access database contains a field that holds date/time values in 'general date' format. These all show up at 01/01/1970 in...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
5
by: Patti | last post by:
I am struggling with converting a certain varchar column into an int. I have a table that has 2 fields - one field holds the loan number and the other field holds the codes associated with that...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
4
by: ll | last post by:
My data input page uses a id generator which includes dashes, and the corresponding column in the db is set up as varchar to handle that. There is a problem, however, when the value in the varchar...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.