Null value is also decoding in HIVE - hive

I am working on Hive. I need decode some of the fields in table so i am using Translate() method but problem is like in some of the places i have null this character also decoding. How do you solve this one?
here is my code
TRANSLATE (Address2,
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'DEFGHIJKLMNOPQRSTUVWXYZABCdefghijklmnopqrstuvwxyzabc') as Address2,

use a CASE block
Select
CASE WHEN Address2 IS NOT NULL THEN
TRANSLATE (Address2,
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'DEFGHIJKLMNOPQRSTUVWXYZABCdefghijklmnopqrstuvwxyzabc')
END as
Address2

Related

SQLite Studio Question for Substr making some of my values disappear?

I'm working with a free dataset from Kaggle to learn SQL further and I've hit a spot where I am stuck. I'm working with an NFL Draft dataset that has player names listed like this:
FirstName LastName\UserName
However, some of the rows are simply this:
FirstName LastName
I wrote this code and have had some success:
SELECT position, substr(Player,0,instr(Player,'\')) AS Player_Name
This specfic code works great on any rows that are formatted like FirstName LastName\UserName but for any rows that are formatted like this FirstName LastName it returns a blank for the Player_Name.
Any tips on how I can fix this to show the FirstName LastName ONLY on my query for both ways?
INSTR returns 0 if the char is not in the string
Supplying a length of 0 causes an output from SUBSTR of zero length string
This is why your name disappears
What we can do is convert the 0 to something else:
SUBSTR(player, 0, COALESCE(NULLIF(INSTR(player, '/'), 0), 9999)
Now if INSTR returns 0, NULLIF converts it to NULL, and COALESCE converts the NULL to 9999
SQLite doesn't care that 9999 is beyond the end of the string (make it bigger if it isn't) and returns the whole rest of string, so effectively the SUBSTR is a non-op
There are other ways to skin the cat:
CASE WHEN player LIKE '%/%' THEN SUBSTR(...) ELSE player END
but they might search the string twice, for lower overall performance.. It might not matter, if it's a handful of values queried infrequently, you might prefer a more readable form over a (theoretically) more performant one

Combine two columns and perform a where operation on the result

I have two columns, one for 'firstName' and one for 'lastName'. I'm trying to perform a where operation on the concatenation of those two columns to match for a full name.
Here is what I've tried:
select * from table where concat_ws(' ', 'firstName', 'lastName') like '%some_value%'
This just returns a blank array.
Example Data:
firstName
lastName
Clayton
Smith
Barbara
Clayman
Sam
Peterson
when some_value = Clay, it should return 'Clayton Smith' and 'Barbara Clayman'
I got a lot of this syntax from some other stackoverflow answers which were marked as solutions to a similar problem, however when implementing this myself I couldn't get it to work.
FWIW I'm going to be building these queries with knex.js so if there's a knex.js specific solution feel free to answer with that.
The arguments to your CONCAT_WS call should be in double quotes. If they're in single quotes, Postgres will interpret them as string literals. This should work:
SELECT * FROM table WHERE CONCAT_WS(' ', "firstName", "lastName") ILIKE '%some_value%'
I also recommend using ILIKE rather than LIKE - this will make the pattern matching for your search term be case insensitve.
use STR_POS
The PostgreSQL strpos() function is used to find the position, from where the substring is being matched within the string.
https://w3resource.com/PostgreSQL/strpos-function.php#:~:text=The%20PostgreSQL%20strpos()%20function,being%20matched%20within%20the%20string.&text=Example%20of%20PostgreSQL%20STRPOS(),within%20the%20argument%20is%205.
select * from table where strpos(concat_ws(' ', first_name, last_name),'Clay') > 0
Replace literal with variable

Check for Lowercased First Character in String Value for MariaDB

In my MariaDB environment I want to check to see if there are any values in the table "contacts" and the column "firstname", where the first letter in the string value is lower case. My question is: will this do the trick?
SELECT * FROM contacts
WHERE (firstname) LIKE '%[abcdefghijklmnopqrstuvwxyz]%';
MySQL does not support syntax [...] with LIKE.
You can also use the following condition to check if the first character of firstname is lowercased:
BINARY lower(left(firstname, 1)) = left(firstname, 1)
Another option is to use a regex (shorter to write and easier to understand):
BINARY firstname RLIKE '^[a-z]'
Please note that both expressions will not use an existing index on firstname, since functions or regexes comes into play.
In order for this to work in general, I believe you have to cast to binary:
cast(firstname as binary) rlike '^[a-z]'
Or:
cast(left(firstname, 1) as binary between 'a' and 'z'

Error: String contains an untranslatable character - TERADATA (for REGEXP_REPLACE operation)

I need to clean one character column and for that I am using REGEXP_REPLACE function in Teradata 14.
The same piece of code worked for some other data source (having the same LATIN encoding).
The data definition using show table has given me below format of the data:
CREATE SET TABLE pp_oap_cj_t.dc_loss_fdr_kn ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
( PARENT_ID DECIMAL(38,0),
FS_MRCH_NM VARCHAR(25) CHARACTER SET LATIN NOT CASESPECIFIC
) PRIMARY INDEX ( PARENT_ID );
The query I am performing is as below:
CREATE TABLE pp_oap_pb_piyush_t.CHECKMERCHANT1 AS (
SELECT
FS_MRCH_NM,
REGEXP_REPLACE(trim(Upper(trim(REGEXP_REPLACE( (FS_MRCH_NM ) , '[^a-z]',' ',1,0,'i'))) ), '[[:space:]]+',' ',1,0,'i') as cleaned_merchant
FROM pp_oap_pb_piyush_t.CHECKMERCHANT)
WITH DATA PRIMARY INDEX (FS_MRCH_NM);
Error
CREATE TABLE Failed. 6706: The string contains an untranslatable character.
I need a quick turnaround this bottleneck.
Help is really appreciated !
Thanks !!!!
REGEXP_REPLACE under the hood converts character set Latin to Unicode. You have defined your variable as character set Latin. You see the error when data has something which cannot be converted from Latin to Unicode. Best thing is to fix your DDL to have character set as Unicode instead of Latin. something like TRANSLATE(FS_MRCH_NM USING LATIN_TO_UNICODE WITH ERROR) in your code instead of FS_MRCH_NM should work. Problem with this it result in null values when you have untranslatable characters.
UPDATE CFSYSUAT.Metadata
SET MetadataTxt =
CASE WHEN TRANSLATE_CHK(MetadataTxt USING UNICODE_TO_LATIN) > 0
THEN SUBSTRING(MetadataTxt,1,TRANSLATE_CHK(MetadataTxt USING UNICODE_TO_LATIN) - 1) ||
SUBSTRING(MetadataTxt,TRANSLATE_CHK(MetadataTxt USING UNICODE_TO_LATIN) + 1)
ELSE MetadataTxt END;
Had some luck with TRANSLATE_CHK.
It returns the position of the offending character.
Used it with SUBSTRING to remove the offending character.
If the text contains multiple bad characters you have to run the update multiple times, each pass will correct another bad character.
HTH,
Nathan
A non-Unicode-compatible version of oreplace is installed in our syslib, and a Unicode-compatible version is in our td_sysfnlib. When the database is not specified, syslib is used before td_sysfnlib. So forcing TD to use the td_sysfnlib version of oreplace solved the problem.
Here's the code used:
SELECT td_sysfnlib.OREPLACE(item_name,'|','') FROM databaseB.sales;
I hope that helps anyone else who's running into the same issue!
TRANSLATE(OREPLACE(TRANSLATE(item_name USING LATIN_TO_UNICODE WITH ERROR),'|','') USING UNICODE_TO_LATIN WITH ERROR) AS LBL

Oracle SQL: Replace all non-numeric values, such as NULL, blank or empty strings

I have a column that is formatted as number but seems to contain some other values as well that are causing trouble with my other functions (Rtrim, Xmlagg, Xmlelement, Extract), leading the whole query to fail (which works well if I take this column out).
Can someone tell me the best way to replace anything in a column that is not a valid number by 0 (or alternatively a placeholder number like 99999) ?
I tried the following but guess this only covers NULL values, not empty strings or blank strings which I think I have to replace first.
TO_CHAR(NVL(a.mycolumn, 0))
Many thanks in advance,
Mike
In Oracle, you should be able to use TRIM():
NVL(TRIM(a.mycolumn), '0')
Oracle (by default) treats empty strings as NULL.
Do note that this will trim the result, even if it is not NULL. If that is not desirable, use CASE:
(CASE WHEN TRIM(a.mycolumn) IS NULL THEN '0' ELSE a.myolumn END)
Use REGEXP_LIKE
For Numeric:
REGEXP_LIKE (numeric_value, '^\d+(\.\d+)?$')
or with null
REGEXP_LIKE(NVL(value, 'NULL'), '^\d+(\.\d+)?$|NULL$')