Solved

mssql - how to get rid of milliseconds in time field?

Posted on 2009-07-01
4
437 Views
Last Modified: 2012-05-07
Dates are currently stored as 2009-01-01 12:12:32.062

I'd like to convert all of the timestamps to drop the milliseconds (2009-01-01 12:12:32)

Is there a way to do that?
0
Comment
Question by:n00b0101
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
you could do this, to set the milliseconds to .000
UPDATE yourtable
  SET yourfield = CONVERT(datetime, CONVERT(varchar(19), yourfield, 120), 120)

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
Comment Utility
Exampple:

DECLARE @T AS dATETIME
SET @T = '2009-01-01 12:12:32.062'
SELECT convert(varchar(19), @t, 121)
P.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Here is one way using date functions:
DATEADD(ms, -DATEPART(ms, GETDATE()), GETDATE())

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
So you could update your table like this:

UPDATE your_table_name
SET your_timestamp = DATEADD(ms, -DATEPART(ms, your_timestamp), your_timestamp);

Or if you meant for a select, then see what Angel Eyes and P proposed.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now