Convert actual time to float in SQL - sql

How can I convert a specific time or actual time, in SQL, to a float number? E.g., if it is 09:59:57 AM I want this float number: 0.416634556 like it is in Excel.
Thank you all.

take a look at this, you may find your answer here: CAST and CONVERT (Transact-SQL)
SELECT CAST(CONVERT(DATETIME, Col1) AS FLOAT) AS NewValue
FROM Table1

SELECT CAST(cast(Col1 as datetime) AS FLOAT) - CAST(cast(Col1 as datetime) AS int) AS decPart
FROM yourTable
try this one

None of the other answers worked for me, but this does: SELECT CAST(CAST(GETDATE() AS DATETIME) AS FLOAT) - FLOOR(CAST(CAST(GETDATE() AS DATETIME) AS FLOAT)) And then you can swap out the GETDATE() calls for your column.

Related

SQL - Convert INT to Date

I am trying to convert INT (YYYYMM) value to DATE.
I have read articles which mention that converts from char to date format. So I am curious to know - which of the foll. is a good approach?
Example:
DECLARE #PERIOD INT ='201806'
SELECT CAST(CONCAT(#Period,'01') AS DATE) SQLCAST1
,CAST(CAST(#Period AS varchar(6))+'01' AS DATE) SQLCAST2
Which is the ideal approach and why? Do we have other better approach?
I am not a fan of implicit data type conversions, so I would phrase the first as:
select convert(date, convert(varchar(6), #PERIOD ) + '01')
Or use datefromparts() which is a built-in function for constructing dates:
select datefromparts( #PERIOD / 100, #PERIOD % 100, 1)
Apparently concat is the most efficient method.
https://raresql.com/2013/03/12/sql-server-string-concatenation-faster-method/
Cast also appears to be the most efficient method for parsing a standard date string (see the try_cast times for DateTime)
SQL - Convert INT to Date
Your first method is most efficient.
Here is another method, First you have to convert to char then convert to date time,
declare #intValue int
select #intValue = 201907
select convert(date, convert(varchar(6), #intValue ) + '01')

TSQL Datetime conversion looses a period of time

i'm playing around with a MsSQL Server for some days now and stumbled upon a strange behaviour when converting a datetime to/from a decimal.
SELECT [date] = GETDATE()
, [as decimal] = CAST(GETDATE() AS decimal)
, [from decimal] = CAST(CAST(GETDATE() AS decimal) AS datetime)
, [as float] = CAST(GETDATE() AS float)
, [from float] = CAST(CAST(GETDATE() AS float) AS datetime)
-- should be: 2009-08-15
, [from stored float] = CAST(CAST('40039.9583333333' AS float) AS datetime)
When i convert a datetime without adding anything specific the decimal will be handled as i would define it decimal(18, 0). So there is a data loss in some way.
If i directly convert a float back to datetime which was converted from a datetime (as shown in line 7 of my query) everything is fine.
But when i load a value from a database table, for example 40039.9583333333 which definitely was calculated from a user input (2009-08-15) and convert it back to a datetime, it'll add one day.
I wasn't able to find anything specific for this time loss.
Is somebody able to describe the problem behind this strange behaviour?
And, if possible: add an example on how to do those conversions correctly?
Thanks.
I used the values you specified '2009-08-15' and converted it to both decimal and float, which resulted in a value of 40038 for both. I used this value to convert back to datetime and both the decimal and float returns '2009-08-15 00:00:00.000'.
The value 40039.9583333333 results in, as Renan just posted :), '2009-08-16 22:59:59.997'.
I would question whether it was 'definitely was calculated from a user input (2009-08-15)' because that does not appear to be the case. There is more to it than shared.
-- Microsoft SQL Server 2014 - 12.0.4100.1 (X64)
DECLARE #dt datetime = '2009-08-15'
SELECT CAST(#dt AS decimal) -- 40038
SELECT CAST(#dt AS float) -- 40038
DECLARE #dec1 decimal = 40038;
SELECT CAST(#dec1 AS datetime) -- 2009-08-15 00:00:00.000
DECLARE #flo1 float = 40038;
SELECT CAST(#flo1 AS datetime) -- 2009-08-15 00:00:00.000
DECLARE #dec2 decimal = 40039.9583333333;
SELECT CAST(#dec2 AS datetime) -- 2009-08-17 00:00:00.000
DECLARE #flo2 float = 40039.9583333333;
SELECT CAST(#flo2 AS datetime) -- 2009-08-16 22:59:59.997
What was your input source into the DB? The underlying issue could be the starting reference point for the date.
As an example: From reading online, in Excel the date value '1900-01-01' is equal to 1 numerically.
However, in SQL Server the date value '1900-01-01' is equal to 0 numerically.
SELECT CAST(CAST('1900-01-01' AS DATETIME) AS FLOAT)
In SQL Server 40039.9583333333 is the result of SELECT CAST(CAST('2009-08-16 23:00' AS DATETIME) AS FLOAT)
You should check the inputs. Check which scale/code is being used to calculate the value that is stored on your table.

Strip year, month and date as int from a datetime field

I need to strip out part of a date (stored in my database table as a datetime field) in the form 'YYYYMMDD'. I would like the output to be an integer.
For example the datetime 2017-03-28 20:44:35.000 would convert to 20170328.
This is my attempt so far:
CAST(
CAST(DATEPART(year,transcriptCreationDateUTC) AS varchar(4))
+ CAST(DATEPART(month,transcriptCreationDateUTC) AS varchar(4))
+ CAST(DATEPART(day,transcriptCreationDateUTC) AS varchar(4))
AS int)
This is ugly to say the least, and also has the issue that it strips out leading zeros in the month and day fields, so for example 2017-03-28 20:44:35.000 converts to 2017328 not 20170328 as I would like.
There must be a better way, any help appreciated!
CONVERT with style 112 (which is YYYYMMDD format):
SELECT CONVERT(VARCHAR(8),transcriptCreationDateUTC,112)
If you really need the result as an INT, wrap in a CAST(... AS INT):
SELECT CAST(CONVERT(VARCHAR(8),transcriptCreationDateUTC,112) AS INT)
Huh? Just convert to a date:
select cast(transcriptCreationDateUTC as date)
If you want this as a string:
select format(transcriptCreationDateUTC, 'yyyyMMdd')
If you are using a pre-2012 version of SQL Server, you can do:
select convert(varchar(10), transcriptCreationDateUTC, 112)
If you want these as an integer (which is not obvious given that your query returns a string), just convert these to an integer:
select cast(format(transcriptCreationDateUTC, 'yyyyMMdd') as int)
Or, use this arithmetic:
select (year(transcriptCreationDateUTC) * 10000 +
month(transcriptCreationDateUTC) * 100 +
day(transcriptCreationDateUTC)
) as yyyymmdd

SQL convert single int to a time value

I am trying to convert a single integer which represents the hour value into a time. I've tried using cast but this converts the value to a date
cast(datepart(hh,tstart) as datetime) as test
I've also tried casting it as a time and the conversion is not allowed.
The numbers I am working with are 7,8,9,10,11,12,13,...,23, 0
The format I would like is convert 7 to 7:00, 23 to 23:00, etc
Thank you
There are many ways to do that:
WITH table_name AS
(
SELECT * FROM (VALUES
(1),(2),(3),(10),(23)
) T(H)
)
SELECT DATEADD(HOUR, H, 0) DateValue,
CONVERT(time, DATEADD(HOUR, H, 0)) TimeValue,
CONVERT(varchar(2), H)+':00' TextValue
FROM table_name T1
I would recomend storing value as TIME datatype.
You can try this:
select cast(dateadd(hour,3,'00:00:00') as time)
which gives result:
03:00:00.0000000
Use your int values in place of 3 in above statement
Use convert datetime to varchar datatype and use case statement
DECLARE #date datetime ='2016-05-01 10:00:000'
SELECT CASE cast(DATEPART(hh,#date)as varchar(10))
WHEN '10'then '10:00'
WHEN '11'then '11:00'
WHEN '12'then '12:00'
.
.
.
WHEN '23' then '23:00'
END 'Hourpart'
You can concatenate a minute to the time and cast it to time
select cast(concat(20,':00') as time(7)) as 'time'
If you wanted to display like 'HH:MM',use the below query.
SELECT CAST(23 AS VARCHAR(50))+':00'
If you wanted to get the result as time format,use the below query.
SELECT CAST(CAST(23 AS VARCHAR(50))+':00:00' AS TIME)
OR
SELECT CAST(DATEADD(HOUR,23,'00:00:00') as time)
here is the sample output :
Assuming all of your potential values are integers between 1 and 24, I think FORMAT is the simplest way.
FORMAT(tstart*100,'00:00')

convert datetime (mm/dd/YYYY) to decimal(YYYYmmDD)

I am trying to get the following result in Sql:
example: 23/05/2014 to 20142305
but get this:
select convert(decimal, convert(datetime, '5/23/2014'))
result:41780
anyone know how you can get the following format?? (if possible ??)
regards and thanks
In many databases (including SQL Server), you can just do:
select 10000 * year(datetime) + 100 * month(datetime) + day(datetime)
Some databases don't support these functions, so you might need extract(datepart from datetime) or a similar function.
You can try this
SELECT CAST(CONVERT(VARCHAR(8), datetime, 112) AS DECIMAL)
CREATE FUNCTION DateToYYYYMMDD
(
#Date DateTime
)
RETURNS decimal
AS
BEGIN
DECLARE #ResultVar decimal
SET #ResultVar=Year(#Date)*10000+Month(#Date)*100+Day(#Date)
RETURN #ResultVar
END
You can use it like this
Select dbo.DateToYYYYMMDD(GetDate())
Remeber,when calling a user function yoy MUST include the schema in this case dbo.
select convert(decimal, format(convert(datetime, '5/23/2014'),'yyyyMMdd'))
SELECT
CONVERT(NUMERIC,
CONCAT(
LEFT('23/05/2014',2)
,RIGHT(LEFT('23/05/2014',5),2)
,RIGHT('23/05/2014',4)
)
)
The CONVERT(NUMERIC part may/not be necessary depending on how the value will be used.