Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir/Madam


how to convert float to timestamp in single select query

for exp. i have float as 1.251152515236 ,

i want to convert this to datetime and from datetime to timestamp... i.e. 02:22:044456

I need sql query


It is actually .crd file made in xml , and i have query inside xml like
XML
<![CDATA[
SELECT USER.LastName, USER.fltime from Tbalname


]>


fltime has float datatype value which i need to convert to time


Regards
Maulik Shah
Posted
Updated 27-Mar-15 1:13am
v2

1 solution

Simple example:
SQL
declare @timeMoment float
set @timeMoment = 1.251152515236
select @timeMoment as floatValue, cast(@timeMoment as DateTime) as datetimeValue


Result:
floatValue	    datetimeValue
1.251152515236	1900-01-02 06:01:39.577


More smooth:
SQL
declare @timeMoment float
declare @datetimeValue datetime
set @timeMoment = 1.251152515236
set @datetimeValue = cast(@timeMoment as DateTime)

select  Right('00' + Cast(DATEPART(hh, @datetimeValue) as varchar(2)), 2) + ':'+
        Right('00' + Cast(DatePart(mi, @datetimeValue) as varchar(2)), 2) + ':'+
        Right('00' + Cast(DatePart(ss, @datetimeValue) as varchar(2)), 2) + '.'+
        Cast(DatePart(ms, @datetimeValue) as varchar)


in SQL Server the long SELECT Right..... ca also be changed to:
SQL
select cast(@datetimevalue as time) test 
 
Share this answer
 
v3
Comments
maulikshah1990 27-Mar-15 7:22am    
i need only 06:01:36.577 from value ..

also how to display this because datatype is float ?
Herman<T>.Instance 27-Mar-15 7:54am    
Improved solution
Herman<T>.Instance 27-Mar-15 7:57am    
to display? the SQL query give the result in varchar result. You don't need to display the float. That's entry for the query
maulikshah1990 27-Mar-15 8:03am    
i tried above cast and i get error as
Database error: [206] [Operand type clash: float is incompatible with time]

i used query as

SELECT tablname.LastName, cast(cast(SUM(tablname.CONVERSATION_TIME) AS datetime) as time) test,
from tablename;

CONVERSATION_TIME is float datatype




Herman<T>.Instance 27-Mar-15 8:34am    
What is the Database you are using? Oracle, SAp/Sybase, MS SQL, ... ?

In MS SQL 2012 the next line works for me:
select cast(cast(@timeMoment as DateTime) as time) testfloat

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900