Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have to store only time in database from textbox.In textbox i insert only time .
what datatype i have to take for this?
ex:i enter start time in textbox and this value is store in database.if anything else ohetthen time value is try enter then msg near textbox display like ...invalid time.
Posted
Comments
vinayraghavendra.hs 30-Jan-13 7:04am    
1.Write the validation for that TextBox which checks for dd-mm-yyyy format.
2.Since TextBox.Text value is always string convert to DateTime and assign to DateTime Variable

You haven't mentioned which database server you are using. If it's SQL Server 2008 or above, you got time[^] as a new datatype. (Note: I am just considered SQL Server here. Oracle and other db may also support this out of the box).

If you using old versions of SQL, you can either store them in DateTime[^] or varchar to store the time. But you will need to extract the time part (either on db side or your .net code) while using it.

Hope it helps!
 
Share this answer
 
Comments
Sandesh M Patil 30-Jan-13 7:43am    
Nicely described Ankur :)
Ankur\m/ 30-Jan-13 7:49am    
Thanks!
Pete O'Hanlon 30-Jan-13 8:09am    
And this is the correct answer (sort of). There is an alternative approach that allows you to store it as an int, which is the number of milliseconds since midnight, but that could be more trouble than it's worth.

Anyhoo, a 5 from me.
Ankur\m/ 30-Jan-13 8:36am    
I never thought of that option. But yes, there will be lot of Maths involved to actually use the data. IMO, varchar seems to very well fit in his scenario as well. Get the value, store it. Just take care the value is valid time. Similarly getting the data back from db and showing it easy too.
And thanks for the vote! :)
Joezer BH 31-Jan-13 3:53am    
5+
When dealing with Time only fields in a database, I usually store them as decimal in the database and use the TimeSpan object to handle them in my applications using the TimeSpan.TotalMilliseconds property.
 
Share this answer
 
You should take Datetime datatype in your database.

Thanks
 
Share this answer
 
Comments
Rahul Rajat Singh 31-Jan-13 4:07am    
If the time datatype is available for you then it is the best choice. But if not then I think datetime should be the way to go. I don't know why the datetime answers are being down voted because it is more type safe and it has merits over storing the data as string.

The second way could be to give the interpretation responsibility to the application and store the time as decimal in the DB(as suggested by Marcus already) but this way the OLAP guys will not work on the DB directly and they will have to rely on the conversion of this value to the time value everytime they need to use it.
use DateTime data type
 
Share this answer
 
Comments
Rahul Rajat Singh 31-Jan-13 4:09am    
same argument here as for the above answer. I support it. Infact the highest voted answer also say the same thing but with some better suggestion.
Ankur\m/ 31-Jan-13 4:27am    
I guess the downvote was for repeating the same answer. The answerer added nothing new.
AshishChaudha 31-Jan-13 4:57am    
I think so..but this solution I had reposted and deleted the first one.The same solution I had given first. I am pasting it as I can view the deleted one

You should take Datetime datatype in your database.

Thanks



Improve solution Permalink

Posted 21 hrs ago
AshishChaudha28.8K
Ankur\m/ 31-Jan-13 5:01am    
Did you delete it? I can still see it there.
AshishChaudha 31-Jan-13 5:34am    
I reposted the same.
hi
check it

DateTime myDate ;
myDate= Convert.ToDateTime(Txtox1.Txt);
 
Share this answer
 
create table demo
(
_date datetime -- declare it as datetime if using sql 2005 in 2008 there is new datatype time
)

// in aspx.cs file
sqlcommand cmd= new sqlcommand("
insert into demo values( convert(nvarchar(10),'"+TextBox1.Text+"',101))",cn);


out put//

_date
2013-01-01 00.00.00
 
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