Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have defined a field in my database table to store time. The default length of the field is 8. I am storing time from TimeODay which is displaying 12:44:10 PM
which overloads the data. How can I store this data to my DateTime field?

Thanks
Posted

If your database field is DateTime, then it stores the data as a number of microseconds since a specific data, so a 12 hour time will not overload the field - it will need to be converted to a DateTime before it is stored.

If your field is varchar then you need to either store your time as 24 hour format (i.e. without the AM/PM indicator) or preferably make it a DateTime field instead.
 
Share this answer
 
datetime is stored properly...
when you want to fetch & display just set format in select statement

for display time in hh:mm:ss tt format...
use this
SQL
select 
convert(varchar,datepart(hh,getdate()) % 12) + ':' + convert(varchar,datepart(Mi,getdate())) +':' + 
convert(varchar,datepart(ss,getdate())) + ' ' +
CASE WHEN DATEPART(HH,getdate()) < 13 THEN 'AM' ELSE 'PM' END


Replace Getdate() with your columnname
Happy Coding!
:)
 
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