Oracle INSTR vs Hive INSTR - sql

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

Related

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()

Big query replace a letter in a string by position

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')

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.

TSQL: substring to include all numbers between 0-9 except 5

I am using the substring function in a case statment to return all numbers between 'B0' - 'B9'
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-9]'
but i do not want to return the number 'B5' in this list. I can get it working as this
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-4]'
WHEN SUBSTRING (postcode, 1, 2) like 'B[6-9]'
but is there a way to add this to one line like the following?
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-9]' and not like 'B5'
EDIT:
how would you do this if you where dealing with numbers between 0-100 so the sql would be like
WHEN SUBSTRING (postcode, 1, 3) like 'B[0-9][0-9]'
but you did not want to include 16 and 17?
WHEN SUBSTRING (postcode, 1, 2) like 'B[012346789]' -- No "5"
LIKE patterns are awesome. Mini regex, if you will.
To carry this out to the tens place or more for numbers, you might want to consider putting these values into a table that you can use for a join, exists or in.
Or, since we're treating the numeric portion of this column as though they were numbers and not a string, this is a sign that they might have been better stored as two columns: one for the alpha, one for the numeric.
Barring any change to your schema, you might remove the alpha characters ad-hoc so you can compare them to a range of numbers, for example:
where cast(replace(postcode, 'B', '') as int) between 0 and 15
or cast(replace(postcode, 'B', '') as int) between 18 and 100
where cast(right(postcode, len(postcode) - 1) as int) between 0 and 15
or cast(right(postcode, len(postcode) - 1) as int) between 18 and 100
These are just a couple of possibilities. You would know best how to massage your data.
You can try to have two ranges, removing the 5 from it:
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-46-9]'
Use ^ to represent Not.. see Like...
but why not Try
`WHEN SUBSTRING (postcode, 1, 2) like 'B[0-4]' Or
SUBSTRING (postcode, 1, 2) like 'B[6-9]'`
or you can put the ranges in the same brackets
`WHEN SUBSTRING (postcode, 1, 2) like 'B[0-46-9]'
For the single digit case (your original question) you got a bunch of great answers already. however without a real regular expression you can't solve the multi digit problem.
It seams that you have a list of values that you want to exclude. I that case you can use this:
WHEN SUBSTRING (postcode, 1, 3) like 'B[0-9][0-9]'
AND SUBSTRING (postcode, 1, 3) NOT IN ('B16', 'B17')
If the list of "excludables" is in a table use NOT EXISTS instead:
WHEN SUBSTRING (postcode, 1, 3) like 'B[0-9][0-9]'
AND NOT EXISTS(SELECT 1 FROM dbo.excludables AS ex WHERE ex.val = SUBSTRING (postcode, 1, 3))

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');