September 4, 2012 at 9:52 am
Hi all,
I have a column of "time" data type. I need to extract it as milliseconds. Is there a function I can use to do that in SS2K8?
Here are some examples of the data stored:
0:12:14:124
0:08:16:361
0:08:57:705
Thanks,
Eugene
September 4, 2012 at 9:56 am
Try DATEPART(ms, YourTimeValue)
Sorry, I need more coffee, I'll come back.
September 4, 2012 at 10:01 am
Try thie:
with testdata as ( -- this cte is only used to provide the test data for the code below
select
cast(stuff(dt.MyTime,8,1,'.') as time) MyTime
from
(values ('0:12:14:124'),('0:08:16:361'),('0:08:57:705')) dt(MyTime)
)
select
datediff(ms,cast('00:00:00' as time), MyTime)
from
testdata;
September 4, 2012 at 10:05 am
Just to give a different answer, but I'd stay with Lynn's function, as it's easier to read/write.
DECLARE @test-2 TABLE( Mytime time)
INSERT INTO @test-2 VALUES ('0:12:14:124'),('0:08:16:361'),('0:08:57:705')
SELECT (DATEPART( HH, Mytime) * 3600000)
+ (DATEPART( MI, Mytime) * 60000)
+ (DATEPART( S, Mytime) * 1000)
+ DATEPART( MS, Mytime)
FROM @test-2
September 4, 2012 at 10:10 am
After seeing Luis's code, retested mine without the stuff, it still works:
with testdata as ( -- this cte is only used to provide the test data for the code below
select
cast(dt.MyTime as time) MyTime
from
(values ('0:12:14:124'),('0:08:16:361'),('0:08:57:705')) dt(MyTime)
)
select
datediff(ms,cast('00:00:00' as time), MyTime)
from
testdata;
September 4, 2012 at 10:19 am
Thanks for replies,
Trying to use your last solution in the query and get an error:
--------------
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string.
----------------
select 'a', 'b', datediff(ms,cast('00:00:00' as time),cast(time_column as time)) time_ms
from T
September 4, 2012 at 10:21 am
eugene.pipko (9/4/2012)
Thanks for replies,Trying to use your last solution in the query and get an error:
--------------
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string.
----------------
select 'a', 'b', datediff(ms,cast('00:00:00' as time),cast(time_column as time)) time_ms
from T
Check your data. About all I can say since I can't see it from here.
September 4, 2012 at 10:22 am
i think this will do what you wanted:
With MyCTE (TheTime)
AS
(
SELECT CONVERT(time,'0:12:14:124') UNION ALL
SELECT '0:08:16:361' UNION ALL
SELECT '0:08:57:705'
)
SELECT *,DATEDIFF(millisecond,cast('00:00:00' as time), TheTime)
From myCTE
Edit:
late to the party; that's what i get for stepping away from my desk; you already got some fine answers before mine.,
Lowell
September 4, 2012 at 10:26 am
Both do the same for me:
with testdata as ( -- this cte is only used to provide the test data for the code below
select
cast(dt.MyTime as time) MyTime
from
(values ('0:12:14:124'),('0:08:16:361'),('0:08:57:705')) dt(MyTime)
)
select
datediff(ms,cast('00:00:00' as time), MyTime)
from
testdata;
with testdata as ( -- this cte is only used to provide the test data for the code below
select
convert(time, dt.MyTime) MyTime
from
(values ('0:12:14:124'),('0:08:16:361'),('0:08:57:705')) dt(MyTime)
)
select
datediff(ms,cast('00:00:00' as time), MyTime)
from
testdata;
September 4, 2012 at 11:39 am
Do you think I am receiving following message because my data is stored in nvarchar(max) column?
--------------
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string.
----------------
select 'a', 'b', datediff(ms,cast('00:00:00' as time),cast(time_column as time)) time_ms
from T
September 4, 2012 at 11:42 am
eugene.pipko (9/4/2012)
Do you think I am receiving following message because my data is stored in nvarchar(max) column?--------------
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string.
----------------
select 'a', 'b', datediff(ms,cast('00:00:00' as time),cast(time_column as time)) time_ms
from T
The error message is telling you that there is data that won't convert from character format to date and/or time format.
September 4, 2012 at 11:45 am
defintiely the problem.
do a SELECT * FROM T WHERE isdate(time_column) = 0
those are the values that cannot be converted, and raise an error.
Lowell
September 4, 2012 at 11:52 am
You're right as I see the following values:
2 days 23:28:45 .703
-4:0-57:0-26:00-703
when running SELECT * FROM T WHERE isdate(time_column) = 0
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply