Both '00'XC in teradata and hive - 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.

Related

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

How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL

I need to find rows where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL.
I am doing like below:
SELECT *
FROM test
WHERE Name_test LIKE '%[^A-Za-z _]%'
But It is not working and I also need to exclude any apostrophe.
Kindly help.
If you need to find all rows where column have ONLY numbers and special characters (and you can specify all of required special characters):
SELECT *
FROM test
WHERE regexp_like(Name_test, q['^[0-9'%##]+$]')
as you can see you just need to add your special characters after 0-9.
^ - start
$ - end
About format q'[SOMETHING]' please see TEXT LITERALS here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-1824CBAA-6E16-4921-B2A6-112FB02248DA
If you need to find all rows where column have no alpha-characters:
SELECT *
FROM test
WHERE regexp_like(Name_test, '^[^a-zA-Z]*$');
or
SELECT *
FROM test
WHERE regexp_like(Name_test, '^\W*$');
about \W - please see "Table 8-5 PERL-Influenced Operators in Oracle SQL Regular Expressions" here:
https://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS235
I need to find rows where col have special characters or numbers (except hyphen, apostrophe and space [and presumably single quotes]) in Oracle SQL.
You can use double single quotes to put a single quote in:
WHERE Name_test LIKE '%[^-A-Za-z _'']%'
However, this is not Oracle syntax. If the above works, then I would guess you are using SQL Server. In Oracle:
WHERE REGEXP_LIKE(Name_test, '[^A-Za-z _'']')

How to remove non-numeric characters (except full stop "." ) from a string in amazon redshift

I have been trying to figure out how to remove multiple non-numeric characters except full stop ("."), or return only the numeric characters with full stop (".") from a string. I've tried:
SELECT regexp_replace('~�$$$1$$#1633,123.60&&!!__!', '[^0-9]+', '')
This query returns following result : 1163312360
But I want the result as 11633123.60
Please try this:
The below regex_replace expression will replace all character which are not ("^") in the (range of 0-9) & "."
SELECT regexp_replace('ABC$$$%%11633123.60','([^0-9.])','') FROM DUAL;
It returns the expected output "11633123.60"

SQL select from list where white space has been added to end

I'm trying to select some rows from an Oracle database like so:
select * from water_level where bore_id in ('85570', '112205','6011','SP068253');
This used to work fine but a recent update has meant that bore_id in water_level has had a bunch of whitespace added to the end for each row. So instead of '6011' it is now '6011 '. The number of space characters added to the end varies from 5 to 11.
Is there a way to edit my query to capture the bore_id in my list, taking account that trialling whitespace should be ignored?
I tried:
select * from water_level where bore_id in ('85570%', '112205%','6011%','SP068253%');
which returns more rows than I want, and
select * from water_level where bore_id in ('85570\s*', '112205\s*','6011\s*', 'SP068253\s*');
which didn't return anything?
Thanks
JP
You should RTRIM the WHERE clause
select * from water_level where RTRIM(bore_id) in ('85570', '112205','6011');
To add to that, RTRIM has an overload which you can pass a second parameter of what to trim, so if the trailing characters weren't spaces, you could remove them. For example if the data looked like 85570xxx, you could use:
select * from water_level where RTRIM(bore_id, 'x') IN ('85570','112205', '6011');
You could use the replace function to remove the spaces
select * from water_level where replace(bore_id, ' ', '') in ('85570', '112205', '6011', 'SP068253');
Although, a better option would be to remove the spaces from the data if they are not supposed to be there or create a view.
I'm guessing bore_id is VARCHAR or VARCHAR2. If it were CHAR, Oracle would use (SQL-standard) blank-padded comparison semantics, which regards 'foo' and 'foo ' as equivalent.
So, another approach is to force comparison as CHARs:
SELECT *
FROM water_level
WHERE CAST(bore_id AS CHAR(16)) IN ('85570', '112205', '6011', 'SP068253');

Concatenate strings in Oracle SQL without a space in between?

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"