Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to save time only to MySql database in format HH:mm eg 13:38
My Code Goes Here..
C#
string strInsert = "Insert into call_log(pno, name, complain_date, complain_time, problem, pcno, complain_no) values('123','name','" + DateTime.Now.ToString("yyyy/MM/dd") + "','" + DateTime.Now.ToString("HH:mm") + "','" + txtProblems.Text + "','" + txtPCno.Text + "','" + callID + "')";
                int effect = GlobalConectionClass.ExecuteNonQuery(strInsert);


it gives Error as--- Incorrect datetime value: '13:38' for column 'complain_time' at row 1

How Can i do it plz Help....
Thanks in Advance!
Posted
Comments
Thomas ktg 7-Sep-13 4:37am    
what is the datatype you have given for complain_time field?
[no name] 7-Sep-13 4:40am    
I M USING DATETIME HERE , SHOULD I USE ANY OTHER HERE,,,,
[no name] 7-Sep-13 4:53am    
you can use here nvarchar or varchar datatype in mysql datatable

There is no data format in MySQL which will save just the Time: it must have a date attached in order to be useful.

The way I would do this is to store it as "minutes since midnight" in an integer, and convert it back to HH:MM when I extracted it. That way, you can do sorting and comparisons etc. and still easily retrieve the values for display.


And please, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
[no name] 7-Sep-13 4:47am    
THANKS FOR UR SUGGESTION!
BUT IF I SAVE THE DATA WITH DATE AND TIME IN SAME COLUMN HOW I WILL FILTER IT DURING SELECT COMMAND BETWEN TWO DATES.
OriginalGriff 7-Sep-13 4:52am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

If you want to rewrite that in proper capitalisation, I might read it, but I am not even going to glance at the content as it is.
[no name] 7-Sep-13 4:53am    
Ok Sir,
[no name] 7-Sep-13 4:55am    
if we save our data using DateTime datatype, with date and time simultaneously, how to filter my select query between two dates.
plz plz Reply.
OriginalGriff 7-Sep-13 5:26am    
That's easy: if you store a single DateTime value with the date and time combined, then all you have to do is use BETWEEN:
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable WHERE MyDateColumn BETWEEN @STARTDATE AND @ENDDATE", con);
cmd.Parameters.AddWithValue("@STARTDATE", startDateAndTime);
cmd.Parameters.AddWithValue("@ENDDATE", DateTime.Now);
If you are using DataType "DATETIME" for Complain Time field, I dont think you can store only the time. Instead you change the DataType into TIME for Complain Time field and use the below format to extract time alone.
Do not pass time string alone and pass the date as Parameter and extract the time using like this.

insert into testtime values(2,TIME('2011-12-31 01:02:03'));

this will take only the time from the given date - "01:02:03".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900