I am looking to put a comma after two digits from right. For example i have 250$ and in SQL I want the select to return 2,50$. Example2: 500$ should be 5.00$. For 1000$. It should be 10,00$. How can it be done in select. I have a column that holds those value in my SQL database.
Thanks you in advance for your help.
My column data type is bigint
Ironically, you can do this the same way in almost any database:
select cast( (col / 100.0) as decimal(20, 2))
Related
I am trying to convert decimal values into non decimal number for a computed column in Oracle SQL.
Please see the below code. But I am not able to get the desired output.
Query:
Select cast(cost_account)*100/cast(amnt_fin) as "computed_LTV"
From Loan_app.
Here I Want a new column name as computed_LTV with the required calculation with no decimal in the output.
It is ROUND you're looking for, I presume.
SELECT ROUND (cost_account * 100 / amnt_fin) AS "computed_LTV" FROM Loan_app
If by "non-decimal number" you mean "integer", just use trunc():
Select trunc(cost_account * 100 / amnt_fin) as computed_LTV
From Loan_app;
I couldn't find any answer to my question and I hope you can help me out. I've been asked to create a revenue report, however, I'm dealing with an old poorly optimized database. It sounds like a simple thing but I cannot find any solution to the below issue.
Basically, some prices are being shown in the following format:
22400
What I need is to show these values in this format format: 22,40
Any suggestions? Thanks in advance.
You can use format function :
select format(prices, '0,000')
from table t;
Try
convert(decimal(38, 2), convert(decimal, 22400) / 1000);
to convert it to a decimal(38, 2). Possibly lower the precision (38) if you don't need that much.
First you have to convert this price(ex: 22400) from one datatype to 'money' datatype using CAST function.
and then CONVERT money to character
references -
1) https://www.techonthenet.com/sql_server/functions/convert.php
2) https://www.techonthenet.com/sql_server/functions/cast.php
Syntax :
CONVERT(nvarchar, CAST(price AS money), 1)
I have a NUMERIC(5, 0) field in a IBM db2 database from which I only want the first 4 digit. I haven't been able to achieve what I want with using CAST.
How can I achieve this in SQL without casting as a String and using substring ?
Ex: 13525 --> 1352
Thanks for your help!
Why not cast as a string and use substring?
You can also do:
select (case when field >= 10000 then floor(field / 10) else field end)
This assuming that if the field has 1234, then you want 1234 rather than 0123.
EDIT:
You can also use a string by using two calls to cast():
select cast(left(cast(field as varchar(5)), 4) as numeric(4, 0))
I should also note that in many databases, you can just do:
select left(field, 4)
and the database will do the appropriate conversions. I don't have DB2 nearby to check this.
I have column in a postgresql database. They are lottery numbers. Four digits in length to be exact. Initially I had the datatype of the column as int. I inserted all the lottery numbers. After I inserted all the numbers I realize it chopped off my zeros. For instance 0925 is 925. I fixed the datatype to be varchar but now I need to figure out how to fix it from int to varchar with the same data. The data needs to be 4 digits in length. I was trying to just figure out how many problem numbers there are and I couldn't write a select statement that told me how many rows have less than 4 digits.
How should I go about this?
Thanks.
I was trying to just figure out how many problem numbers there are and I couldn't write a select statement that told me how many rows have less than 4 digits.
SELECT COUNT(*)
FROM lottery
WHERE char_length(x) < 4
See it working online: sqlfiddle
To fix them, you may find lpad useful. Note that the WHERE clause is not actually needed.
UPDATE lottery
SET x = lpad(x, 4, '0')
See it working online: sqlfiddle
Format your numbers with to_char():
SELECT to_char(123, 'FM0000');
You might even just leave them as integer and use the expression in queries.
Or, to convert your column back from integer to text in place:
ALTER TABLE tbl ALTER column col TYPE text USING to_char(col, 'FM0000');
Since you seem to have already converted the numbers to varchar, the expression needs an additional cast to integer:
SELECT to_char(col::int, 'FM0000')
FROM tbl;
I have a question that bothers me. How can i convert a varchar to number when inside the varchar value consists of alphabets.
For my varchar price column values:
14dollars10cents
15dollars20cents
By converting it to varchar to number price column, the values should be:
1410
1520
I know that if the varchar does not consists any alphabets, it can auto convert by"
SELECT CONVERT(INT, PRICE) FROM Table
Is there any way to get rid of the alphabets in the middle as I would like to do mathematical function on it.
Updated attempt of putting fixed point number in:
SELECT CAST (Replace(REPLACE(PRICE, 'dollars', '.'),'cents','') AS Number(4,2)))
FROM TEST;
Thanks
You could just use REGEXP_REPLACE to remove all non digit characters:
SELECT REGEXP_REPLACE(price, '[^[:digit:]]')
FROM table;
To then convert this to a number:
SELECT TO_NUMBER(REGEXP_REPLACE(price, '[^[:digit:]]'))
FROM table;
If you want to add the point in then you can do that with REGEXP_REPLACE too:
SELECT TO_NUMBER(REGEXP_REPLACE(val, '^([0-9]*)([^[:digit:]]*)([0-9]*)(.*)$', '\1.\3'))
FROM table;
Voila...
SELECT CAST(REPLACE(YourVarcharCol, 'dollars', '') AS INT) FROM Table
The issue with this is it will break if the varchar still contains alpha-numeric characters.
How about using translate to strip out the unwanted characters.
SELECT TO_NUMBER(TRANSLATE('14dollars10cents','1234567890dolarscents','1234567890')) FROM DUAL
No I don't think there is direct way.
you can do string parsing to get your integer value.