MySQL function to select float value as integer if its decimal point is 0 - ruby-on-rails-3

I'm having a column of float type(amount). The value can be an integer or float . I want to get the value as integer if the decimal point is 0 (eg: value = 0.0, 1.0 etc) and need the value as decimal itself if it has got decimal points. Is there any MySQL function for this?
The following is my query
SELECT GROUP_CONCAT(CONCAT(reason_code, ':', COALESCE(identifier, ''), '*', amount * -1)) AS accout_groups
FROM adjustments WHERE check_id = 1 GROUP BY department_id*
Result of query is below
PI:*-900.00,AP:*-87.00,CS:12312312*-1500.00,SL:12312312*-1103.83
What Iam expecting is
PI:*-900,AP:*-87,CS:12312312*-1500,SL:12312312*-1103.83

I tried using
SELECT GROUP_CONCAT(CONCAT(reason_code, ':', COALESCE(identifier, ''), '*',(TRIM(TRAILING '.' FROM(TRIM(TRAILING '0' FROM amount)))) * -1)) AS accout_groups FROM adjustments WHERE check_id = 1 GROUP BY department_id
This is working

Related

how to add trailing or leading zero to `0.1201` or `01.00` total 7 numbers in Postgresql

how to add trailing or leading zero to 0.1201 or 1.00 total 7 numbers in Postgresql?
supposed I have data of below, and we have to store numbers in a varchar column(and this is not a broken database design, we also have some values of 02.34x924 in this column, could you please tell me how to save them to numeric or double)
numbers
0.1201
1.00
26.25
02.34x924
I expected output is, on the left of decimal point two numbers, and on the right of decimal point are four numbers, including decimal point total 7 numbers
numbers
00.1201
01.0000
26.2500
02.34x924
I can use below SQL to let 26.25 equal to 26.2500, but I can't deal with 0.1201 1.00
select
case when length(numbers) < 7 then rpad(numbers, 7, '0') else numbers end numbersnew
from mytable
I would split the value into two parts then pad each part individually.
select concat(lpad(p1, 2, '0'),
'.', p2,
rpad('0', 4 - length(p2), '0'))
from (
select trim(split_part(numbers, '.', 1)) as p1,
trim(split_part(numbers, '.', 2)) as p2
from my_table
) t
The derived table is mainly there to avoid repeating the expression to get each part.
I would put that into a function to make your queries easier to read:
create function format_my_string(p_input text)
returns text
as
$$
select concat(lpad(p1, 2, '0'),
'.', p2,
rpad('0', 4 - length(p2), '0'))
from (
select trim(split_part(p_input, '.', 1)) as p1,
trim(split_part(p_input, '.', 2)) as p2
) t;
$$
language sql
immutable;
Then use it like this:
select format_my_string(numbers)
from my_table

SQL Cast to show decimals

I have the following statement within my select clause;
(([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as Total_Completed
How do I implement CAST "cast(your_float_column as decimal(10,2))" ? I want my column Total_Completed to show 2 decimal places
I cannot seem to get the correct syntax!
Thank you
the result of the calculation depends of the used columns type.
If you divide int columns, you get int result : 1 / 6 = 0
when you convert each values to decimal the result is: 1 / 6 = 0.1666666666666
Now you want 2 decimal result,so you have to convert/ round the previous result to get the expected value
See fiddle for some example of divide and cast / round : http://sqlfiddle.com/#!18/51785/5
An easy trick can be to use :
round ( 1.0 * ( [Complt_Emp] + [No_Non_Complt_Emp] ) / [No_of_Emp] , 2 )
Cast each expression seperately
CAST(([Complt_Emp] + [No_Non_Complt_Emp]) as decimal(10,2)) /
CAST([No_of_Emp] as decimal(10,2)) as Total_Completed
I suspect all your values are INT. An int divided by an int will return an int.
(([Complt_Emp] + [No_Non_Complt_Emp])/ cast([No_of_Emp] as decimal(10,2)) as Total_Completed
Try this
cast((([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as decimal(10,2)) as Total_Completed

SQL SELECT statement with Cast, LPad and Max

Are there any issues with this select statement ??
SELECT SUBSTR(FIELD_A,10,3) as MOVID,
MAX(LPAD((CAST(SUBSTR(FIELD_A,-3,3) as INT) + 1 ), 3, 0)) as NEXTMOVID
FROM ...
The field is a VARCHAR2.
I want to maintain 3 characters(which are numbers) and add 1 and concatenate with another varchar2
retrieve a portion of the FIELD_A
convert is to an integer
add 1
Left Pad it to 3 characters with 0
Grab the MAX
Later on I concatenate with another field
Wondering if there was a better way to do this ??
As I understand, you need following:
SELECT SUBSTR(FIELD_A,10,3) as MOVID,
to_char(to_number(SUBSTR(FIELD_A, -3, 3)) + 1), '000') as NEXTMOVID
FROM ...

Remove trailing zero from decimal number

I have a one database table field called Amount which type is decimal(18,6). so it is stored in database up to 6 decimal points like 9.786534 But while retrieving that field using select query i have to take care like following
Remove trialling zero e.g if number is 9.230000 then result is only 9.23
If decimal points are all zero then only remove only four trialling zero e.g If number is 9.000000 then result is 9.00
Result is up to 2 decimal point if there are trialling zero.
If we write simple query like
select TOP 1 Amount From EmployeeMaster
then it gives 9.230000
but my intension is to remove trailing zero..
Please help me..
It works for removing trailing zeros, but I am still not able to convert 9 to 9.00 in this method.
Declare #myvalue varchar(50),
#Price Varchar(50)
Set #Price = '9.230000'
set #Myvalue = reverse(substring(#Price,patindex('%.%',#Price)+1,len(#Price)))
SELECT
case
When patindex('%.%[1-9]%',#price) = 0 Then
substring(#price,1,patindex('%.%',#price)-1)
else
substring(#price,1,patindex('%.%',#price)-1) + '.' + Reverse(substring(#Myvalue,patindex('%[1-9]%',#Myvalue),len(#Myvalue)))
END
Coming from decimal(18,6) you could do...
select cast(Amount as decimal(18,2))
Most databases that support the CAST function will round the number while converting it. On SQLServer this is what I would do if I wanted rounding.
If what you actually want is a string with only two digits after the decimal then you could
select cast((Amount as decimal(18,2)) as nvarchar)
nvarchar is SQLServer's variable length unicode type. Databases do not agree much on string types. Your database may have a different one. The rest of that sql is ANSI standard. Not all dbs support that either but many do.
This should work
SELECT CAST(REPLACE(RTRIM(REPLACE(CAST(CAST(33.9082976 AS DECIMAL(38,8)) AS NVARCHAR(256)),'0',' ')),' ','0') AS FLOAT)
Does this work?
select TOP 1 ROUND(Amount, 2) From EmployeeMaster
TRY below mentioned code.
SELECT TOP 1 CONVERT(DECIMAL(10,2),Amount) From EmployeeMaster
Hope it will work as expected.
An alternative approach:
1) convert the decimal to a string;
2) split the string into 2 parts, separating the last 4 characters from the rest of the string;
3) remove trailing zeros from the last 4 characters;
4) concatenate the two parts back.
WITH data (V) AS (SELECT CAST(9.786534 AS decimal(18,6))
UNION ALL
SELECT CAST(9.78653 AS decimal(18,6))
UNION ALL
SELECT CAST(9.7800 AS decimal(18,6))
UNION ALL
SELECT CAST(9.7 AS decimal(18,6))
UNION ALL
SELECT CAST(9.00000 AS decimal(18,6))
)
, AsString (V) AS (SELECT CAST(V AS varchar) FROM data)
, Split (L, R) AS (SELECT LEFT(V, LEN(V) - 4), RIGHT(V, 4) FROM AsString)
, Adjusted AS (SELECT L,
REPLACE(RTRIM(REPLACE(R, '0', ' ')), ' ', '0') AS R
FROM Split)
SELECT Result = L + R FROM Adjusted
The output of the above script is:
Result
--------
9.786534
9.78653
9.78
9.70
9.00
I guess using patindex in your case:
CASE WHEN FLOOR(Amount) <> CEILING(Amount) THEN
LTRIM(SUBSTRING(STR(Amount, 18, 6), 1, LEN(STR(Amount, 18, 6)) - PATINDEX('%[^0]%', REVERSE(str(Amount, 18, 6))) + 1))
ELSE STR(Amount,18,2)
END
for a decimal(18,6) field this should work:
select trim(to_char(Amount, '999999999999999999.99')) from EmployeeMaster
(at least for Oracle, not sure about other types)

SQL - How do i output string with numbers in sql?

I want to get a number 5000.1 and divide it by 1000 before adding an "F" infront of it.
How do i do this? I tried and failed this:
select "F" + round ( acq.store_size_net / 1000, 0) from acq
I suspect your missing the cast of the number to a text data type
Without knowing the exact dialect of sql you're using im gonna hazard a guess at ms-sql
select 'F' + cast(cast(round ( 5000.1 / 1000, 0)as int) as nvarchar(50))
produces output
F5
This will work in Oracle :
select 'F' || round (acq.store_size_net / 1000, 0) from acq