Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omitting the Milliseconds in a Date

When I select from SQL Server, I want to get a date, but omit the millisecond value, and I want it to be as a date type. So if I have a value 1/1/2009 1:23:11.923, I want to omit the millisecond but retain the date type, so that it will be the value 1/1/2009 1:23:11.000 (I know you really can't omit the millisecond value with a date, just want it to be zero).

Is there a function in SQL Server to do this? Or do I have to write my own function? Again, I don't want it as a varchar type, but a datetime type.

like image 795
Brian Mains Avatar asked Feb 11 '10 18:02

Brian Mains


People also ask

How do I remove milliseconds from a timestamp?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) . The setSeconds method takes the seconds and milliseconds as parameters and sets the provided values on the date.

How do you date milliseconds?

JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.

What is small date time?

SMALLDATETIME is a domain, implemented as TIMESTAMP, used to store date and time of day information. SMALLDATETIME is a Transact-SQL type.


2 Answers

Use DATETIME2, a new datatype in SQL Server 2008 that supports fractional precision:

SELECT   CONVERT(DATETIME2(0),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss] , CONVERT(DATETIME2(1),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.f] , CONVERT(DATETIME2(2),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ff] , CONVERT(DATETIME2(3),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fff] , CONVERT(DATETIME2(4),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ffff] , CONVERT(DATETIME2(5),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fffff] , CONVERT(DATETIME2(6),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ffffff] , CONVERT(DATETIME2(7),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fffffff] 

The conversion will round to the nearest unit, eg:

2014-09-04 09:35:47.0162993 as DATETIME2(4) ->  2014-09-04 09:35:47.0163 

Alternatively, on SQL 2005 and eariler:

SELECT   original  = GETDATE() , [floor]   = DATEADD(ms,-DATEPART(ms,GETDATE()),GETDATE()) , [ceiling] = DATEADD(ms,1000-DATEPART(ms,GETDATE()),GETDATE()) , [rounded] = DATEADD(ms,CASE WHEN DATEPART(ms,GETDATE()) < 500 THEN 0 ELSE 1000 END-DATEPART(ms,GETDATE()),GETDATE()) 

This is a bit faster than converting to and from a string representation.

like image 173
Peter Radocchia Avatar answered Sep 23 '22 21:09

Peter Radocchia


Use:

SELECT CONVERT(DATETIME, CONVERT(VARCHAR(19), GETDATE(), 120)) 

This:

CONVERT(VARCHAR(19), GETDATE(), 120) 

...omits the milliseconds, returning a VARCHAR. So you CAST/CONVERT that into a DATETIME in order to work with the desired data type.

See this link for a list of various date/time formats you can work with.

like image 31
OMG Ponies Avatar answered Sep 23 '22 21:09

OMG Ponies