?
Solved

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

Posted on 2009-07-01
4
Medium Priority
?
483 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

752 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