Solved

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

Posted on 2009-07-01
4
464 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24757095
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
ID: 24757096
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
ID: 24757098
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
ID: 24757111
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 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