Related
Here is a example query, I've been trying to find a workaround for this but my knowledge for postgresql at this time is still limited. Thanks in advance.
Sample Query:
select round(3.041,2) as column
Expected Output:
3.03 instead of 3.04
Rounding UP works but I need it to round DOWN as well if the decimal value is <5.
You can round down with some simple decimal counting like
SELECT
3.04 - POWER(10,
(-1 *
CAST(
LENGTH(
SPLIT_PART(
CAST(3.04 AS STRING), '.', 2
)
)
AS INT)
)
)
However, I have no idea why you would do this. Are you sure you aren't trying to use the basic ROUND function and cut off more significant figures?
I'm working in a project and in order to know how much time happened between two dates i'm using the following code:
select (DATE_COMPLETED - DATE_STARTED) as avg_days
The problem is that when I run the code it shows the new column but the format answer is for example:
{"months": 0, "days": 0, "microseconds": 2803077000000}
As you may infer I want that information in days, not in 'microseconds'
By the way, the format of the Date data I'm using is '2021-02-24T02:33:00.000+0000'.
Thanks!
If you get microseconds and you want days, use arithmetic:
select (DATE_COMPLETED - DATE_STARTED) / (24 * 60 * 60 * 1000000) as avg_days
I have been trying to achieve this all day, I have followed numerous tutorials and can't seem to crack it, I have been trying things like:
select CONVERT(VARCHAR(10), DATE, 131) from Table
yet it does not seem to change anything.
Any advice or help would be appreciated. Thankyou in advance.
SELECT CONVERT(VARCHAR(10), DATE, 12)
12 is the right code for the format you want. See: https://msdn.microsoft.com/en-GB/library/ms187928.aspx
Try this
declare #TDATE Date = '2015-11-10';
select Convert(varchar,Datepart(Year, #TDATE))+Convert(varchar,Datepart(Month, #TDATE))+Convert(varchar,Datepart(Day, #TDATE))
Output:
20151110
12 is the right code. Use below query to get output as 'yymmdd'
select CONVERT(VARCHAR(10), DATE, 12) from Table
On PostgreSQL the easiest way is to use to_char:
to_char(date, 'yyyymmdd')::int
One method is to construct the value from date parts. Here is a numeric conversion:
select (year(date) % 100) * 10000) + month(date) * 100 + day(date)
It is easy to convert this to a number:
select cast( (year(date) % 100) * 10000) + month(date) * 100 + day(date) as varchar(10))
The slight advantage to this approach over using convert is that a human being can understand the logic without perusing arcane documentation, specific to SQL Server. I don't know why SQL Server doesn't support a simple format-type function similar to most other databases (and programming languages).
select date_format(column_name, '%Y%m%d') from table_name;
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')
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')