I have a query as below:
Select price from myTable;
This returns value '22.50'
However, I need the format as 5.8 bytes in my program.
That is my String value should be '00022.50000000' and not '22.50'
Is there a way to achieve this directly in SQL ?
From docs, I think you want CHAR( DECIMAL( 22.50, 13, 8 ) )
I have no DB2 instance to play with so can't verify. These functions are not ANSI SQL, they are DB2 specific.
Related
I have phone numbers in a SQL database stored in (xxx) xxx-xxxx format. is there any way to cast these as xxxxxxxxxx format instead?
Simply you can use REPLACE() function :
SELECT REPLACE(REPLACE(REPLACE(REPLACE('(XXX) XXX-XXXX','(',''),')',''),'-',''),' ','')
Assuming you are using Oracle or MySQL 8+, you could use REGEXP_REPLACE:
SELECT
REGEXP_REPLACE('(914) 591-8563', '\((\d{3})\)\s*(\d{3})-(\d{4})', '\1\2\3')
FROM dual;
9145918563
This solution works via regex by capturing the 10 digits, and then generating a replacement of only digits. Explore the demo below using either Oracle or MySQL 8+:
Demo
I need to convert varchar2 type value 100.0 into 100 (without decimal points) in Oracle SQL. Can you please help me?I used regexp_substr...but it fails in one situation given below.
SELECT REGEXP_SUBSTR(0.1,'(\d*)') FROM DUAL;---it results in Null but i want zero(0).
Note:- in Orcale sql developer
You want to display number in format You wish it to be displayed, so You should use formating functions. No need for rounding here in my opinion - query below should give You what You want.
SELECT
TO_CHAR(TO_NUMBER('100.1'),'FM9999') as res
FROM
dual
;
More on number formats can be found here.
Try to this....
SELECT TRUNC(TO_NUMBER(100.5)) FROM DUAL;
I have tried many combinations of the SQL functions; so as to have a 12 digit number including the dot character, including leading zeroes and decimal points.
For example:
for the number 121.22, I want to format it to 000000121.22
or for the number 12.2, I want to format it to 000000012.20
or for the number 100, I want to format it to 000000100.00
I have used the following function; but I lost the decimal points if it's zero.
SELECT RIGHT('000000000000'+ STR(CONVERT(VARCHAR,MYNUMBER),12,2),12);
Any idea on how to solve this problem in Microsoft SQL?
If you're on SQL Server 2012 or later, you can use the format() function.
SELECT FORMAT(121.22, '000000000000.00')
SELECT FORMAT(12.2, '000000000000.00')
000000000121.22
000000000012.20
for ms sql versions not in (2012,2014):
cast(right('000000000',9-len(floor(the_number))) as varchar)
+ cast( cast(the_number as decimal(10,2))as varchar)
for ms sql versions in (2012,2014):
format(the_number ,'000000000000.00')
SELECT padded_id = REPLACE(STR(id, 12), SPACE(1), '0')
Is what I add to use (In SQL server) leading 0's as needed, change the 12 to whatever total number of digits you want it to be.
This allows for non hard coded values, just make sure id or whatever column/param you want to format is set.
I am trying to query a column of type (int) into a string of format (000). For example: If the column value was 1 then the output of the query should be 001
Depends on which database engine you use.
In SQL Server 2012 and higher:
SELECT FORMAT(7, 'D3')
The result is this string:
007
For older versions of SQL Server, see the accepted answer by shree.pat18
For MySQL, look here: Adding a leading zero to some values in column
For PostgreSQL, look here: Padding zeros to the left in postgreSQL
Try this:
select right('000' + convert(varchar(3), intcolumn), 3) from yourtable
Note that the output is of type varchar. If you will need this output as a number somewhere else, I would suggest doing the formatting in your UI code and keeping it as a number in the query.
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.