Big query replace a letter in a string by position - sql

I would to like to replace a letter in a string by position in big query.
for example XXXXX, YYYYY, ZZZZZ the 5th letter in the string to 0
I've tried to use the Stuff function, but big query doesn't find the function
Stuff(XXXXX, 5, 1, '0')

Probably the simplest method is more basic string operations:
select concat(substr(x, 1, 4), '0', substr(x, 6))

Below is for BigQuery Standard SQL and Legacy SQL (works for both)
SELECT REGEXP_REPLACE(str, r'(.{4}).(.*)', r'\10\2')

Related

Oracle INSTR vs Hive INSTR

I have Oracle-query and need to use the same in Hive.
select INSTR('some string', 's', 1, 1) - INSTR('some string', 's', 1, 2);
>> -5
but Hive doesn't have INSTR like Oracle.
I can use only this one:
select INSTR('some string', 's');
>> 1
and it finds first substring position in string but I need find the second or third and etc.
Oracle query has specific logic thats why I can't change it.
What can I do with it?
This was added as part of https://issues.apache.org/jira/browse/HIVE-25230 in Hive 4 alpha 2

Removing part of the string

SQL....On my table I have attribute table with “Pat0700-1700” on my report I want to drop the Pat and only display 0700-1700. How would I accomplish this on SQL. I have search and tried the substring with neg results.
On these following RDBMS:
Oracle
MySQL
DB2
StandardSQL
you can try with the function SUBSTR():
SELECT SUBSTR(<column>, 4) AS substr_string
FROM <table>
OUTPUT:
substr_string
-------------
0700-1700
The standard SQL method would be replace():
select replace(col, 'Pat', '')
Given that the rest of the string has a fixed format -- 9 characters -- you might also find that one of these is appropriate (and more general):
select right(col, 9)
select substr(col, 4, 9) -- or perhaps substring()

How to get file name without extension with using Regular Expressions

I have a field with following values, now i want to extract only those rows with "xyz" in the field value mentioned below, can you please help?
Mydata_xyz_aug21
Mydata2_zzz_aug22
Mydata3_xyz_aug33
One more requirement
I want to extract only "aIBM_MyProjectFile" from following string below, can you please help me with this?
finaldata/mydata/aIBM_MyProjectFile.exe.ld
I've tried this but it didn't work.
select
regexp_substr('FinalProject/MyProject/aIBM_MyProjectFile.exe.ld','([^/]*)[\.]') exp
from dual;
To extract substrings between the first pair of underscores, you need to use
regexp_substr('Mydata_xyz_aug21','_([^_]+)_', 1, 1, NULL, 1)
To get the file name without the extension, you need
regexp_substr('FinalProject/MyProject/aIBM_MyProjectFile.exe.ld','.*/([^.]+)', 1, 1, NULL, 1)
Note that each regex contains a capturing group (a pattern inside (...)) and this value is accessed with the last 1 argument to the regexp_substr function.
The _([^_]+)_ pattern finds the first _, then places 1 or more chars other than _ into Group 1 and then matches another _.
The .*/([^.]+) pattern matches the whole text up to the last /, then captures 1 or more chars other than . into Group 1 using ([^.]+).
For the first requirement, it would suffice to use LIKE, as posted in answer above:
SELECT column
FROM table
WHERE column LIKE '%xyz%';
For your second requirement (extraction) you will have to use REGEXP_SUBSTR function:
SELECT REGEXP_SUBSTR ('FinalProject/MyProject/aIBM_MyProjectFile.exe.ld', '.*/([^.]+)', 1, 1, NULL, 1)
FROM DUAL
I hope it helped!
Another way to do this is to skip regexp completely:
WITH
aset AS
(SELECT 'with_extension.txt' txt FROM DUAL
UNION ALL
SELECT 'without_extension' FROM DUAL)
SELECT CASE
WHEN INSTR (txt, '.', -1) > 0
THEN
SUBSTR (txt, 1, INSTR (txt, '.', -1) - 1)
ELSE
txt
END
txt
FROM aset
The result of this is
with_extension
without_extension
A BIG Caveat where the regexp is better:
My method doesn't handle this case correctly:
\this\is.a\test
So after I have gone to all this effort, stay with the regexp solutions. I'll leave this here so that others may learn from it.

Removal of first characters in a string oracle sql

It may be very simple question, but I have run out of ideas.
I would like to remove first 5 characters from string.
Example string will look like:
1Y40K100R
I would like to display only digits that are after '%K' which in this case should give me result of 100R.
Please note that number after 'K' can have different amount of digits. It can be 4 digit number or 2 digit number.
Just use substr():
select substr(col, 6)
This returns all characters starting at the sixth.
There are multiple ways to return all characters after the k. If you know the string has a k, then use instr():
select substr(col, instr(col, 'K') + 1)
You can use regexp_substr
select regexp_substr('1Y40K100R', '(K)(.*)', 1, 1, 'i', 2) from dual
A way without regexp:
select substr('1Y40K100R', instr('1Y40K100R', 'K') +1) from dual
This may appear not so elegant, but it usually performs better than the regexp way.

How to take last four characters from a varchar?

I'm trying to take the last four characters only from a varchar field. All the rows are different lengths. What function should I be using to accomplish this?
Right should do:
select RIGHT('abcdeffff',4)
SUBSTR(column, LENGTH(column) - 3, 4)
LENGTH returns length of string and SUBSTR returns 4 characters from "the position length - 4"
RIGHT ( character_expression , integer_expression )
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
Use the RIGHT() function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
For Oracle SQL, SUBSTR(column_name, -# of characters requested) will extract last three characters for a given query. e.g.
SELECT SUBSTR(description,-3) FROM student.course;
You can select last characters with -
WHERE SUBSTR('Hello world', -4)
I have Used RIGHT function in SQL Server and it's working.
SELECT RIGHT( your_column_name, 4 );
--It will show last 4 digit/char
You can just use
SELECT RIGHT(<Column_name>, 4) FROM <table_name>
This will be giving you all the lines but only with their last 4 characters.
tested solution on hackerrank....
select distinct(city) from station
where substr(lower(city), length(city), 1) in ('a', 'e', 'i', 'o', 'u') and substr(lower(city), 1, 1) in ('a', 'e', 'i', 'o', 'u');