Concatenate strings in Oracle SQL without a space in between? - sql

I am trying to concatenate strings in oracle.
The following is my query:
insert into dummy values('c'||to_char(10000,'99999'));
The expected result is:
c10000
But the output I get is with a space in between 'c' and the value 10000:
c 10000
How to concat without spaces?

This is not an issue with the concatenation operator but with the function to_char(). Try instead:
to_char(10000,'FM99999')
I quote the manual here:
FM ..
Returns a value with no leading or trailing blanks.

There are two solutions:
Fill Mode ('FM') formatting prefix that suppresses the additional blank character prefix for the to_char number conversion. I suggest this one is preferred, because it is integrated with the to_char format and does not require an additional function call;
LTRIM of the returned value from the to_char number conversion.
The code below shows the results of both solutions:
Select concat('NTA', to_char(1,'FM0000000000000')),
concat('NTA', ltrim(to_char(1,'0000000000000'))),
concat('NTA', to_char(1,'0000000000000'))
from dual;
"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))": "NTA0000000000001"
"CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))": "NTA0000000000001"
"CONCAT('NTA',TO_CHAR(1,'0000000000000'))": "NTA 0000000000001"

Related

Both '00'XC in teradata and hive

I found the following query in teradata and there is no error when I run it.
Trim(Both '00'XC FROM STG_101_104_BORM_NEW.NO_TRN) AS NO_TRN
what is the use of Both '00'XC function?
when I run it on hive there is an error, what should be changed when we translate it to hive?
Trim(Both '00'XC FROM STG_101_104_BORM_NEW.NO_TRN) will work in Teradata and it removes leading and trailing null hexadecimal characters from the string.
'00'XC - is a null character. XC indicates a hexadecimal string literal and 00 is null in hex.
So trim() is trying to remove it but this will work in teradata but not in hive. You need to know the purpose of this function in teradta and then implement accordingly in hive.
I explained how trim() is different in hive and teradata below.
trim(origstr) in hive just removes spaces from left and right of a string.
trim(both/leading/trailing patternstr from origstr)) in Teradata also removes spaces(and other strings) from the string but it needs more arguments. its more powerful because it can remove bytes/hex characters as well etc.
Teradata-Trim
For example in hive-
SELECT TRIM(' aabccd ') FROM t2; - will result aabccd
For example in teradata-
SELECT TRIM(BOTH FROM ' aabccd ') FROM t2; - will result aabccd
SELECT TRIM(LEADING 'a' FROM 'aabccd') FROM t2; - will result bccd
EDIT :
please use to remove leading and trailing 0.
select REGEXP_REPLACE(REGEXP_REPLACE('000ABCA000','0+$',''),"^0+(?!$)",'')
'0+$' - this will remove trailing 0s.
^0+(?!$) - this will remove leading 0s.

Remove template text on regexp_replace in Oracle's SQL

I am trying to remove template text like &#x; or &#xx; or &#xxx; from long string
Note: x / xx / xxx - is number, The length of the number is unknown, The cell type is CLOB
for example:
SELECT 'H'ello wor±ld' FROM dual
A desirable result:
Hello world
I know that regexp_replace should be used, But how do you use this function to remove this text?
You can use
SELECT REGEXP_REPLACE(col,'&&#\d+;')
FROM t
where
& is put twice to provide escaping for the substitution character
\d represents digits and the following + provides the multiple occurrences of them
ending the pattern with ;
or just use a single ampersand ('&#\d+;') for the pattern as in the case of Demo , since an ampersand has a special meaning for Oracle, a usage is a bit problematic.
In case you wanted to remove the entities because you don't know how to replace them by their character values, here is a solution:
UTL_I18N.UNESCAPE_REFERENCE( xmlquery( 'the_double_quoted_original_string' RETURNING content).getStringVal() )
In other words, the original 'H'ello wor±ld' should be passed to XMLQUERY as '"H'ello wor±ld"'.
And the result will be 'H'ello wo±ld'

How to handle string with only space in oracle sql?

I have a case where I am getting the data from DB and converting the string to a number using TO_NUMBER, but this case fails when the string is an empty string with unknown or space char like
columnA
------
4444
333333
The string '4444' and '333333' is converted to number by there is and error "ora-01722 invalid number" for the 2nd string.
Can this be handled with DECODE or CAST in any way, because I need to use TO_NUMBER any how for further processing?
I hope this could be Insight of your issue.
select
TO_NUMBER(trim(colA)),
TO_NUMBER(REGEXP_REPLACE(colA,'(^[[:space:]]*|[[:space:]]*$)')),
regexp_instr(colA, '[0-9.]')
from
(
select ' 123' colA from dual
union all
select ' ' colA from dual
union all
select '.456' colA from dual
)
This is similar issue : Trim Whitespaces (New Line and Tab space) in a String in Oracle
If all the data within that column is composed of integers, integers with leading and/or trailing whitespaces, null values and only whitespaces then only using TRIM() function will suffice such as
SELECT TRIM(columnA)
FROM t
and that would be more performant than using functions of regular expressions
But
If the data contains decimal numbers, letters, punctiations and special characters along with whitespaces and null values, then use
SELECT TRIM('.' FROM REGEXP_REPLACE(columnA,'[^[:digit:].]'))
FROM t
where there is at most one dot character assumed to be between the starting and ending digits. All of the leading and trailing dots are trimmed at the end of the operation provided there is any of them. The other characters are already removed by the regular expression.
If you're sure that there's no trailing or leading dots, then using
SELECT REGEXP_REPLACE(columnA,'[^[:digit:].]')
FROM t
would be enough
Demo
You can wrap up any of the expressions with TO_NUMBER() function depending on your case at the end

Extract number between two characters in Hive SQL

The query below outputs 1642575.0. But I only want 1642575 (just the number without the decimal and the zero following it). The number of delimited values in the field varies. The only constant is that there's always only one number with a decimal. I was trying to write a regexp function to extract the number between " and ..
How would I revise my regexp_extract function to get the desired output? Thank you!
select regexp_extract('{"1244644": "1642575.0", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*');
You can cast the result to bigint.
select cast(regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*') as bigint) col;
output - 1642575
You can use round if you want to round it off.
select round(regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','([1-9][0-9]*[.][0-9]+)&*')) col;
output - 1642576
Use this regexp: '"(\\d+)\\.' - means double-quote, capturing group with one or more digits, dot.
select regexp_extract('{"1244644": "1642575.9", "1338410": "1650435"}','"(\\d+)\\.',1)
Result:
1642575
To skip any number of leading zeroes, use this regexp: '"0*(\\d+)\\.'

Unwanted Spaces when concatenating fields and text strings

I'm putting the following in my SQL select statement to concatenate text strings (which are not fields in the database) with a couple database fields and I'm getting spaces where I try to use the to_char function to add leading zeros to a couple fields.
Running:
SELECT 'EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00') as ATS_NBR
Yields:
EP. 0017092. 01
How to I eliminate the unnecessary spaces?
Try using replace function:
SELECT replace(('EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00')), ' ', '') as ATS_NBR