Oracle LPAD() function - oracle

Question: For every part description that begins with the letter β€œb”, list the part description, and then pad each part description with a β€œ+”on the left side so that all these part descriptions are 15 characters in length.
And I wrote like
SELECT
LENGTH(PART_PART_DESCRIPTION), LPAD(PART_PART_DESCRIPTION,15,'+'),
PART_PART_DESCRIPTION, CONCAT('+', PART_PART_DESCRIPTION) FROM PART
WHERE SUBSTR(PART_PART_DESCRIPTION,1,1)='B'
but the output doesn't show 15 of '+' on left side.
Here is the output table

Your column PART_PART_DESCRIPTION is of CHAR data type with 285 data length. so BLENDER in your column has a total 285 (7 + 278 trailing spaces) length. that is why you are facing the problem.
See this:
SQL> select LPAD(CAST('BLENDER' AS CHAR(285)),15,'+') FROM DUAL;
LPAD(CAST('BLENDER'ASCHAR(285)),15,'+')
------------------------------------------------------------
BLENDER
SQL> select LPAD('BLENDER',15,'+') FROM DUAL;
LPAD('BLENDER',
---------------
++++++++BLENDER
SQL>
You need to use TRIM to properly use the LPAD on CHAR datatype column Something like the following:
LPAD(trim(PART_PART_DESCRIPTION),15,'+')

Most probably your data is padded with spaces. Try this
SELECT
LENGTH(PART_PART_DESCRIPTION), LPAD(TRIM(PART_PART_DESCRIPTION),15,'+'),
PART_PART_DESCRIPTION, CONCAT('+', PART_PART_DESCRIPTION) FROM PART
WHERE SUBSTR(PART_PART_DESCRIPTION,1,1)='B'

Related

Oracle APEX number format mask without comma

I am trying to change the number eg. 1000 to 1.000, 10000 to 10.000 istead of 10,000 or 10,000.00
DO you have any idea? :)
As far as I can tell, there's (unfortunately) no declarative way to modify thousands/decimal separators. Should be somewhere in "Edit application definition - Globalization" settings, but - there's nothing like that there.
So, do it yourself, manually. Navigate to shared components - security attributes - database session tab - initialization PL/SQL code and put this into it:
begin
execute immediate q'[alter session set nls_numeric_characters = ', ']';
end;
which will set comma as decimal separator, and space as thousands (group) separator.
Example:
SQL> alter session set nls_numeric_characters = ', ';
Session altered.
SQL> select 5000 val1,
2 to_char(5000, '999G990D00') val2,
3 to_char(5000, '999G990') val3
4 from dual;
VAL1 VAL2 VAL3
---------- ----------- --------
5000 5 000,00 5 000
SQL>
You can use
SELECT TO_CHAR(your_nr_col, '999G999G999G990', 'nls_numeric_characters='',.''') AS nr_formatted
FROM t
if you're dealing with integers only ( without including any decimal separator ), put as much as 9s to exceed the number of the digits while adding Gs after each three padded digit starting from the right hand side.
If there might arise some cases in which the decimal separators are needed, then replace the second argument with '999G999G999G990D0000' to pad enough 0s after single D character directing from left to right
DemO

concatenation of strings in oracle apex

I have problem to display picture in PL/SQL Dynamic view Content (Apex Oracle).
Below is problematic row of code:
sys.htp.p( '<img src="'https://apex.oracle.com/pls/apex/cgdev/test/cool/'||'kk1.ID'||'.1.png'"/>');
where is kk1.ID is ID relevant to report.
I need to get result:
"https://apex.oracle.com/pls/apex/cgdev/test/cool/2.1.png"
How can I concatenate strings in the right way?
Regards,
Stefan
Line #5 shows how it should look like. Your problem was in wrong enclosing kk1.id into single quotes, I think. I left opening and closing IMG tags out. Add them, if necessary.
SQL> with kk1 (id) as
2 (select 2 from dual)
3 select
4 '"https://apex.oracle.com/pls/apex/cgdev/test/cool/' || kk1.ID ||'.1.png"' col
5 from kk1;
COL
----------------------------------------------------------
"https://apex.oracle.com/pls/apex/cgdev/test/cool/2.1.png"
SQL>

Supress leading zeros from oracle table extract to a file

I am extracting data from oracle table to a text file and I have below number columns. When I select the below columns to a file it gives me all leading zeros which I wanted to suppress.
Select ltrim(col_1,'0'),ltrim(col_2,'0'),ltrim(col_3,'0') from table1
Datatype:
Col_1 ---NUMBER(10,2),
Col_2 ---NUMBER(38,0),
Col_3 ---NUMBER(15,1)
Current Output:
00000303.44|0| 00000000000008.2
00000000.00|26| 00000000000030.2
00000473.40|0| 00000000000010.0
Expected Output:
303.44|0|8.2
0|26|30.2
473.4|0|10
Please let me know if i need to change the datatype to get the Expected output. I even tried TO_CHAR(TRIM(LEADING 0 FROM col_name) i did not get the expected output.
This is caused by the datatypes set in the last output stage of your datastage job. When a column is set a decimal, datastage will fill the remaining positions with leading zeros up to the size if your decimal field.
The easiest way to get around this is to place a transform prior to the file output stage and convert all the columns to a varchar at the last stage trimming all the leading zeros.
Since the data is not in number and possibly in varchar/varchar2;
conversion is required; you can use to_number to address this;
Using one of your sample data in below case
select
to_number(00000000000008.2) as num1,
to_number('00000000000008.2') as chr1,
trim(00000000000008.2) as num2,
trim('00000000000008.2') as chr2,
ltrim(00000000000008.2,'0') as num3,
ltrim('00000000000008.2','0') as char3
from dual

Oracle comment to Find first charater start with Alpha and second with numeric

I have table called DOMcolumn name position
I have below list of values in Position column
SN125A,A1234,SSD123,B12347,SDDF234,11123,E1O123,B12345
my requirement is to find the positions whose First letter is Alpha and second letter is numeric.
So My expectation Positions is
A1234,B12347,E1O123,B12345
Once I find these position then I need to remove the first character of the above positions
So my final output should be :
1234,12347,1O123,12345 (removed the alpha from above positions)
How do we achieve this in Oracle.
I tried the below REGEXP_LIKE(POSITIONS, '[A-Za-z]') in my query but I'm not sure about it.
You should match the beginning of the line ^ and also test for following digits
^[A-Za-z][0-9]
You can so add a third parameter to make the matching case-insensitive:
REGEXP_LIKE (POSITIONS, '^[A-Z][0-9]', 'i');
You can remove the first char by using SUBSTR
SUBSTR(POSITIONS, 2) AS pos
dom is the table name, position is the column name
select * from dom;
position
----------
SN125A
A1234
SSD123
B12347
SDDF234
11123
E1O123
B12345
8 rows selected.
1 select substr(position, 2,length(position)) from dom
2* where regexp_like(position,'^[[:alpha:]][[:digit:]]')
SQL> /
SUBSTR(position,2,LENGTH(position))
--------------------------------------------
1234
12347
1O123
12345

Oracle cursor removes leading zero

I have a cursor which selects date from column with NUMBER type containg floating point numbers. Numbers like 4,3433 are returned properly while numbers smaller then 1 have removed leading zero.
For example number 0,4513 is returned as ,4513.
When I execute select used in the cursor on the database, numbers are formatted properly, with leading zeros.
This is how I loop over the recors returned by the cursor:
FOR c_data IN cursor_name(p_date) LOOP
...
END LOOP;
Any ideas why it works that way?
Thank you in advance.
You're confusing number format and number value.
The two strings 0.123 and .123, when read as a number, are mathematically equals. They represent the same number. In Oracle the true number representation is never displayed directly, we always convert a number to a character to display it, either implicitly or explicitly with a function.
You assume that a number between 0 and 1 should be represented with a leading 0, but this is not true by default, it depends on how you ask this number to be displayed. If you don't want unexpected outcome, you have to be explicit when displaying numbers/dates, for example:
to_char(your_number, '9990.99');
It's the default number formatting that Oracle provides.
If you want to specify something custom, you shall use TO_CHAR function (either in SQL query or PL/SQL code inside the loop).
Here is how it works:
SQL>
SQL> WITH aa AS (
2 select 1.3232 NUM from dual UNION ALL
3 select 1.3232 NUM from dual UNION ALL
4 select 332.323 NUM from dual UNION ALL
5 select 0.3232 NUM from dual
6 )
7 select NUM, to_char(NUM, 'FM999990D9999999') FORMATTED from aa
8 /
NUM FORMATTED
---------- ---------------
1.3232 1.3232
1.3232 1.3232
332.323 332.323
.3232 0.3232
SQL>
In this example, 'FM' - suppresses extra blanks, '0' indicates number digit including leading/trailing zeros, and '9' indicates digit suppressing leading/trailing zeros.
You can find many examples here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570

Resources