Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sqlquerystress - To test db performance 8 39
migrate a SQL 2008 to 2016, 2 26
transaction in asp.net, sql server 6 31
sql server service accounts 4 19
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

813 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

10 Experts available now in Live!

Get 1:1 Help Now