Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to insert only time in sqlserver.

My table contains column Logintime datatype Time

when i am inserting in to table it store as

12:56:11.0630000
07:30:21.4230000
13:21:32.7200000


but i want only time like 12:56:11


my C#.net code is :-
C#
Datetime logintime;
obj.logintime= DateTime.Now.ToLocalTime();

please help me
Posted
Updated 1-Dec-11 21:40pm
v2
Comments
[no name] 2-Dec-11 3:40am    
EDIT: added "pre" and "b" tags

Use time(7) as your datatype

-Eduard
 
Share this answer
 
v2
Comments
Member 7932936 2-Dec-11 3:48am    
i used like time(7) only
[no name] 2-Dec-11 3:51am    
my bad. it cannot happen. time is saved in database completely including milliseconds. You could remove those milliseconds ONLY for VIEWING purposes. See the ff links:
http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx

http://msdn.microsoft.com/en-us/library/bb677243.aspx
Whatever you store in a DateTime field in SQL server (or .NET itself) is stored as a number of milliseconds since an arbitrary start point. Unless you store it as a string value (and that is a very bad idea), it will always have a decimal component to the seconds. Leav it alone: and handle human readable conversions when you display the data, do not try to remove information to make it simpler to display later - it won't work, it will just make things worse!
 
Share this answer
 
Comments
[no name] 2-Dec-11 3:45am    
my 5. But incase of viewing, see these links http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx and http://msdn.microsoft.com/en-us/library/bb677243.aspx
Amir Mahfoozi 2-Dec-11 4:05am    
+5 I agree with you.
In Database Add as string


obj.logintime=DateTime.Now.ToShortTimeString()
 
Share this answer
 
ToShortTimeString() function in Asp.net will retrieve only time. You can use that eg. DateTime.Now.ToShortTimeString()
 
Share this answer
 
v2
SQL
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond as TIME go
 
Share this answer
 
Read the following first:
The new data types in SQL Server 2008
Following is similar thread:
http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/76281bc8-a97a-46df-a78a-4864ba4cdb1b/[^]

In addition time will save in database including milliseconds. But you need format as you desire when you view those. Following is the link that described better:
time in SQL Server 2008
 
Share this answer
 
v2

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