Solved

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

Posted on 2009-07-01
4
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60

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 60

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

742 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