In my database table fields are saved as 2013-02-15 00:00:00.000. I want they should be 2013-02-15 23:59:59.999. So how to convert 2013-02-15 00:00:00.000 to 02-15 23:59:59.999. In other words only change minimum to maximum time.
Asked
Active
Viewed 1.4k times
2
-
2why you want to do such a thing? – evilone Apr 09 '14 at 17:49
-
Because my business logic code testing the time – Imran Qadir Baksh - Baloch Apr 09 '14 at 17:50
-
What kind of testing? I bet it's better to be done at comparasing then modifying data – Yuriy Galanter Apr 09 '14 at 17:53
-
@YuriyGalanter, In have live app which include a bug that need to be fixed quickly. My db have 00:00:00.000 in time field. My business check current date with current time which is causing this issue. – Imran Qadir Baksh - Baloch Apr 09 '14 at 17:55
-
What datatype are you using? In SQL 2012, it can make a big difference. – Philip Kelley Apr 09 '14 at 17:57
-
@PhilipKelley, datetime – Imran Qadir Baksh - Baloch Apr 09 '14 at 17:57
-
Then you'll have to use .997 -- is that bad? (More detailed answer pending, though someone will probably beat me to it) – Philip Kelley Apr 09 '14 at 17:58
-
@PhilipKelley, cusious to see your answer – Imran Qadir Baksh - Baloch Apr 09 '14 at 18:00
3 Answers
4
DECLARE @Time TIME = '23:59:59.999'
SELECT dateColumn + @Time
FROM tableName
Edit
Cast @time to datetime before (+)
DECLARE @Time TIME = '23:59:59.999'
SELECT dateColumn + CAST(@Time as DATETIME)
FROM tableName

Mudassir Hasan
- 28,083
- 20
- 99
- 133
-
1I think due to http://stackoverflow.com/questions/715432/why-is-sql-server-losing-a-millisecond – Imran Qadir Baksh - Baloch Apr 09 '14 at 17:59
-
@user960567 thanks for pointing out the thread..didn't know about precesion – Mudassir Hasan Apr 09 '14 at 18:01
-
1I am using SQL Server 2014 and getting this error. `Msg 402, Level 16, State 1, Line 2 The data types datetime and time are incompatible in the add operator.` – abhi Apr 09 '14 at 18:03
-
2
Easily done:
SELECT dateCol + '23:59:59'

T McKeown
- 12,971
- 1
- 25
- 32
-
-
I think due to http://stackoverflow.com/questions/715432/why-is-sql-server-losing-a-millisecond – Imran Qadir Baksh - Baloch Apr 09 '14 at 17:58
2
The datetime datatype is not accurate to thousandths of seconds, but rather only to 333rds of a second (details in BOL, it has to do with how the data is actually stored).
Run this for an example:
DECLARE @Test datetime = 'Feb 15, 2013'
PRINT convert(varchar(50), @Test, 109)
SET @Test = dateadd(ms, -1, @Test)
PRINT convert(varchar(50), @Test, 109)
SET @Test = dateadd(ms, -1, @Test)
PRINT convert(varchar(50), @Test, 109)
SET @Test = dateadd(ms, -1, @Test)
PRINT convert(varchar(50), @Test, 109)
PRINT ''
SET @Test = dateadd(ms, -3, @Test)
PRINT convert(varchar(50), @Test, 109)
SET @Test = dateadd(ms, -3, @Test)
PRINT convert(varchar(50), @Test, 109)
SET @Test = dateadd(ms, -3, @Test)
PRINT convert(varchar(50), @Test, 109)
This generates:
Feb 15 2013 12:00:00:000AM
Feb 15 2013 12:00:00:000AM
Feb 15 2013 12:00:00:000AM
Feb 15 2013 12:00:00:000AM
Feb 14 2013 11:59:59:997PM
Feb 14 2013 11:59:59:993PM
Feb 14 2013 11:59:59:990PM
So, the simplest way to update a datetime to the highest/last possible moment prior to, say, Feb 15, 2013, you need to subtract 3 milliseconds, as shown above. In a table, it would be something like:
UPDATE MyTable
set MyDateTime = dateadd(ms, -3, MyDateTime)

Philip Kelley
- 39,426
- 11
- 57
- 92
-
SQL 2012 introduced datatypes that provide extreme precision for time, if you need them. – Philip Kelley Apr 09 '14 at 18:07