I need to convert minutes to hours, rounded off to two decimal places. I also need to display only up to two numbers after the decimal point. So if I have minutes as 650, then hours should be 10.83.
Here's what I have so far:
Select round(Minutes/60.0,2) from ....
But in this case, if my minutes is, say, 630 - hours is 10.5000000. But I want it as 10.50 only (after rounding). How do I achieve this?
You could cast your result as numeric(x,2). Where x <= 38.
select
round(630/60.0,2),
cast(round(630/60.0,2) as numeric(36,2))
Returns
10.500000 10.50
With SQL Server 2012, you can use the built-in format function:
SELECT FORMAT(Minutes/60.0, 'N2')
You can use:
select cast((630/60.0) as decimal(16,2))
in SQL Server
Declare #number float = 35.44987665;
Select round(#number,2)
CAST(QuantityLevel AS NUMERIC(18,2))
Convert your number to a Numeric or Decimal.
Replace your query with the following.
SQL Server
Select Convert(Numeric(38, 2), Minutes/60.0) from ....
MySQL:
Select Convert(Minutes/60.0, Decimal(65, 2)) from ....
The Cast function is a wrapper for the Convert function. Couple that with SQL being an interpreted language and the result is that even though the two functions produce the same results, there is slightly more going on behind the scenes in the Cast function.
Using the Convert function is a small saving, but small savings multiply. The parameters for Numeric and Decimal (38, 2) and (65, 2) represent the maximum precision level and decimal places to use.
DECLARE #porcentaje FLOAT
SET #porcentaje = (CONVERT(DECIMAL,ABS(8700)) * 100) / CONVERT(DECIMAL,ABS(37020))
SELECT #porcentaje
Try this:
SELECT CAST(ROUND([Amount 1]/60,2) AS DECIMAL(10,2)) as TOTAL
Following query is useful and simple-
declare #floatExchRate float;
set #floatExchRate=(select convert(decimal(10, 2), 0.2548712))
select #floatExchRate
Gives output as 0.25.
This works in both with PostgreSQL and Oracle:
SELECT ename, sal, round(((sal * .15 + comm) /12),2)
FROM emp where job = 'SALESMAN'
Whatever you use in denomination should be in decimal. For example, 1548/100 will give 15.00.
If we replace 100 with 100.0 in our example then we will get 15.48
select 1548/100
15.00000
select 1548/100.0
15.4800
0
As an add-on to the answers below, when using INT or non-decimal datatypes in your formulas, remember to multiply the value by 1 and the number of decimals you prefer.
I.e. - TotalPackages is an INT, and so is the denominator TotalContainers, but I want my result to have up to six decimal places.
Thus:
((m.TotalPackages * 1.000000) / m.TotalContainers) AS Packages,
The following snippet might help you:
select SUBSTR(ENDDTTM,1, 9), extract(DAY FROM (ENDDTTM)), ENDDTTM, BEGINDTTM, (ENDDTTM - BEGINDTTM),substr(BEGINDTTM, 1,15), substr((ENDDTTM - BEGINDTTM), 12, 8),
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 3600 + substr((ENDDTTM - BEGINDTTM), 15, 2)*60 + substr((ENDDTTM - BEGINDTTM), 18, 2)),2) as seconds,
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 60 + substr((ENDDTTM - BEGINDTTM), 15, 2) + substr((ENDDTTM - BEGINDTTM), 18, 2)/60 ), 2)as minutes,
round((substr((ENDDTTM - BEGINDTTM), 12, 2) + substr((ENDDTTM - BEGINDTTM), 15, 2)/60 + substr((ENDDTTM - BEGINDTTM), 18, 2)/3600 ),2) as hours
I find the STR function the cleanest means of accomplishing this.
SELECT STR(ceiling(123.415432875), 6, 2)
To round up to x decimal places:
SET #Result = CEILING(#Value * POWER(10, #Decimals)) / POWER(10, #Decimals)
where #Value is the value of the item to be rounded, #Decimals is the number of decimal places, for example, two in this instance.
This worked for me:
SELECT FORMAT(Minutes/60.0, '0.00')
Related
On Teradata SQL I am trying to convert the duration 30 hours and 30 minutes given as '3030' to 1) 30:30 and 2) 30.5 . Or, in another example 3 hours and 15 minutes as 1) 3:10 and 3) 3.25
I can use mod and other arithmetic operations. However, is there a default way of doing this in Teradata SQL?
Every interval has a FORMAT
select format(interval '3:30' hour to minute)
returns
-h(1):mm
but I never found a way to change it :-(
Assuming that you input is a string:
cast(col as int) / 100 + to_number(col) mod 100 / 60 -- numeric result
trim(cast(col as int format 'Z(8)9:99')) -- string result
You can just insert the character:
select substr(val, 1, 2) || ':' substr(val, 3, 2)
Or, convert the values to integers:
select cast(substr(val, 1, 2) as decimal(10, 2)) + cast(substr(val, 3, 2) as decimal(10, 2))/60.0
EDIT:
If you want to convert them to a number, you can do:
select (cast(hhmm as int) / 100 + (cast(hhmm as int) mod 100) / 60.0
This will return a number.
I am calculating total hours/minutes but i would like to get rid off the decimals and only show something like this 2.00 hours or 2.5 hours etc. I am getting now something like this: 2.000000 and want only to limit to 2 decimals only.
select DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours
from myTable
where userid = 123
You can do it by rounding but the easiest is to format for output using FORMAT().
select FORMAT(DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0, 'N2') as hours
from myTable
where userid = 123
Helpful original documentation: here
try use
cast('values' as decimal(18,2)) --2 decimal place.
select Cast((DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours)as decimal(18,2))
from myTable
where userid = 123
There are a few options out there.
I prefer to use the following when no rounding is needed
FORMAT(value, 'N2')
SQL - Rounding off to 2 decimal places
how to get 2 digits after decimal point in tsql?
just use ROUND function such as : SELECT ROUND(columnName,decimals) from table
You could use STR or CONVERT function:
DECLARE #v NUMERIC(19, 6)
SET #v = 2.189189
SELECT STR(#v, 19, 2) AS Method1_Result_VARCHAR, CONVERT(NUMERIC(15, 2), #v) AS Method2_Result_NUMERIC
/*
Method1_Result_VARCHAR Method2_Result_NUMERIC
---------------------- ----------------------
2.19 2.19
*/
Note: First argument of STR function has float type and this means that 1) SQL Server will convert this argument from numeric to float and 2) method 1 uses a non-deterministic expression.
I have Minutes 140. Which in hours and minutes becomes 2:20.
In this case i would love to get 2.33.
What i've tried:
select cast(140/60 as varchar(8)) + '.' + cast((140 % 60) as varchar(8))
Outputs: 2.20
select 140/60
Outputs: 2
select cast(140/60 as decimal(5,2))
Outputs: 2.00
What am i missing?
How do i convert 140 minutes to represent hours decimally?
Sorry about the quick comment, i'll try to explain a little more clearly about this.
By default, Sql will "think" that both your dividend & divisor are INT data type, that's why it returns 2.
If you specify the number with decimal, like this :
select (140.00/60.00)
now the data type is not int any more, and the result is : 2.3333333
So, you will need to convert one of the data type to float, decimal, numeric(n, n) to get the accurate result :
select cast(140 as decimal(5, 2)) / cast(60 as decimal(5, 2))
But you still can just convert only dividend or divisor, like this :
select 140 / cast(60 as decimal(5, 2))
or
select cast(140 as decimal(5, 2)) / 60
they both gave the same result, becasue the result type is the data type of the argument with the higher precedence, in this case, decimal has the higher precedence than int
you can read more here :
Divide
Data Type Precedence
Try this
Select convert(decimal(5,2),convert(float,140)/60)
or
Select cast(140.00/60 as decimal(5,2))
Part of the task I have been given involves performing calculations on a few columns, 2 of which are in the format of hh.mi.ss and they're varchar. In order for the calculations to work, I need to get them into a time decimal format, whereby 1:30 would be 1.5 . Since I'm currently using SQL Server 2005, I don't have the time or data types built-in and I'm unable to get an upgraded version (not my choice). Working with what I have, I've searched around online and tried to convert it but the result isn't accurate. For example, 13.28 becomes (roughly) 13.5, which is great, however, the seconds go to 100 instead of ending at 60 (since I'm converting it to a float).
For example, using 12.57.46,
CAST(DATEPART(HH, CAST(REPLACE([OASTIM], '.', ':') AS DATETIME)) AS FLOAT) +
(CAST(DATEPART(MI, CAST(REPLACE([OASTIM], '.', ':') AS DATETIME)) AS FLOAT)/60) +
(CAST(DATEPART(SS, CAST(REPLACE([OASTIM], '.', ':') AS DATETIME)) AS FLOAT)/3600)
gave me 12.962...
whereas
CAST(SUBSTRING([OASTIM], 1, 2) AS FLOAT) +
((CAST(SUBSTRING([OASTIM], 4, 5) AS FLOAT) +
CAST(SUBSTRING([OASTIM], 7, 8) AS FLOAT)/60)/60)
gave me 12.970....
and when I tried something simpler,
DATEPART(HOUR, CAST(REPLACE([OASTIM], '.', ':') AS DATETIME))+
(DATEPART(MINUTE, CAST(REPLACE([OASTIM], '.', ':') AS DATETIME))/60)
flopped out and gave me only 12
It's my first exposure to Windows SQL and T-SQL, I've been struggling with this for a few hours. As horrible as it sounds, I'm at the point where I'd be happy with it working even it it means sacrificing performance.
You don't explain what "time decimal" format is. From your example, I'll guess that you mean decimal hours.
A key function in SQL Server for date differences is datediff(). You can convert the time to seconds using a trick. Add the time to a date, then use datediff() to get the number of seconds after midnight. After that, the conversion to decimal hours is just arithmetic.
Here is an example:
select datediff(second,
cast('2000-01-01' as datetime),
cast('2000-01-01 ' + '00:00:59' as datetime)
)/3600.0 as DecimalHours
Note the use of the constant 3600.0. The decimal point is quite important, because SQL Server does integer division on integer inputs. So, 1/2 is 0, rather than 0.5.
You said,
CAST(SUBSTRING([OASTIM], 1, 2) AS FLOAT) +
((CAST(SUBSTRING([OASTIM], 4, 5) AS FLOAT) +
CAST(SUBSTRING([OASTIM], 7, 8) AS FLOAT)/60)/60)
gave me 12.970....
12.970 is wrong for an input of '12.57.46'. The problem is that you are using the SUBSTRING function incorrectly. The 3rd argument represents the number of characters, not the ending character position.
Take a look at this code:
Declare #Sample varchar(20)
Set #Sample = '12.57.46'
select CAST(SUBSTRING(#Sample, 1, 2) AS FLOAT) +
CAST(SUBSTRING(#Sample, 4, 5) AS FLOAT) / 60 +
CAST(SUBSTRING(#Sample, 7, 8) AS FLOAT) / 60 / 60,
SUBSTRING(#Sample, 1, 2),
SUBSTRING(#Sample, 4, 5),
SUBSTRING(#Sample, 7, 8),
CAST(SUBSTRING(#Sample, 1, 2) AS FLOAT) +
CAST(SUBSTRING(#Sample, 4, 2) AS FLOAT) / 60 +
CAST(SUBSTRING(#Sample, 7, 2) AS FLOAT) / 60 / 60
Notice that the minutes is coming out as 57.46 because you are asking for 5 characters. The seconds are coming out correctly because eventhough you are asking for 8 characters, there are only 2 characters left in the string so only those 2 characters are returned.
BTW, I would solve this problem the same way that Gordon did, except I would remove the date stuff so it would look like this:
Select DateDiff(Second,
0,
Convert(DateTime, Replace([OASTIM], '.',':'))) / 3600.0
Here is a simple way to convert time to DecimalHours.
SELECT cast(cast('12.57:46' as datetime) as float) * 24
Result:
~12.963
I need to convert minutes to hours, rounded off to two decimal places. I also need to display only up to two numbers after the decimal point. So if I have minutes as 650, then hours should be 10.83.
Here's what I have so far:
Select round(Minutes/60.0,2) from ....
But in this case, if my minutes is, say, 630 - hours is 10.5000000. But I want it as 10.50 only (after rounding). How do I achieve this?
You could cast your result as numeric(x,2). Where x <= 38.
select
round(630/60.0,2),
cast(round(630/60.0,2) as numeric(36,2))
Returns
10.500000 10.50
With SQL Server 2012, you can use the built-in format function:
SELECT FORMAT(Minutes/60.0, 'N2')
You can use:
select cast((630/60.0) as decimal(16,2))
in SQL Server
Declare #number float = 35.44987665;
Select round(#number,2)
CAST(QuantityLevel AS NUMERIC(18,2))
Convert your number to a Numeric or Decimal.
Replace your query with the following.
SQL Server
Select Convert(Numeric(38, 2), Minutes/60.0) from ....
MySQL:
Select Convert(Minutes/60.0, Decimal(65, 2)) from ....
The Cast function is a wrapper for the Convert function. Couple that with SQL being an interpreted language and the result is that even though the two functions produce the same results, there is slightly more going on behind the scenes in the Cast function.
Using the Convert function is a small saving, but small savings multiply. The parameters for Numeric and Decimal (38, 2) and (65, 2) represent the maximum precision level and decimal places to use.
DECLARE #porcentaje FLOAT
SET #porcentaje = (CONVERT(DECIMAL,ABS(8700)) * 100) / CONVERT(DECIMAL,ABS(37020))
SELECT #porcentaje
Try this:
SELECT CAST(ROUND([Amount 1]/60,2) AS DECIMAL(10,2)) as TOTAL
Following query is useful and simple-
declare #floatExchRate float;
set #floatExchRate=(select convert(decimal(10, 2), 0.2548712))
select #floatExchRate
Gives output as 0.25.
This works in both with PostgreSQL and Oracle:
SELECT ename, sal, round(((sal * .15 + comm) /12),2)
FROM emp where job = 'SALESMAN'
Whatever you use in denomination should be in decimal. For example, 1548/100 will give 15.00.
If we replace 100 with 100.0 in our example then we will get 15.48
select 1548/100
15.00000
select 1548/100.0
15.4800
0
As an add-on to the answers below, when using INT or non-decimal datatypes in your formulas, remember to multiply the value by 1 and the number of decimals you prefer.
I.e. - TotalPackages is an INT, and so is the denominator TotalContainers, but I want my result to have up to six decimal places.
Thus:
((m.TotalPackages * 1.000000) / m.TotalContainers) AS Packages,
The following snippet might help you:
select SUBSTR(ENDDTTM,1, 9), extract(DAY FROM (ENDDTTM)), ENDDTTM, BEGINDTTM, (ENDDTTM - BEGINDTTM),substr(BEGINDTTM, 1,15), substr((ENDDTTM - BEGINDTTM), 12, 8),
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 3600 + substr((ENDDTTM - BEGINDTTM), 15, 2)*60 + substr((ENDDTTM - BEGINDTTM), 18, 2)),2) as seconds,
round((substr((ENDDTTM - BEGINDTTM), 12, 2)* 60 + substr((ENDDTTM - BEGINDTTM), 15, 2) + substr((ENDDTTM - BEGINDTTM), 18, 2)/60 ), 2)as minutes,
round((substr((ENDDTTM - BEGINDTTM), 12, 2) + substr((ENDDTTM - BEGINDTTM), 15, 2)/60 + substr((ENDDTTM - BEGINDTTM), 18, 2)/3600 ),2) as hours
I find the STR function the cleanest means of accomplishing this.
SELECT STR(ceiling(123.415432875), 6, 2)
To round up to x decimal places:
SET #Result = CEILING(#Value * POWER(10, #Decimals)) / POWER(10, #Decimals)
where #Value is the value of the item to be rounded, #Decimals is the number of decimal places, for example, two in this instance.
This worked for me:
SELECT FORMAT(Minutes/60.0, '0.00')