How to use split_part function in HIVESql - sql

Here is what i do in prestosql
select split_part('one|||two','|||',1)
result = 'one'
but I can't use this function(split_part) in HIVEsql
Is there any possible function for me to get the same result as above but in HIVEsql?

Maybe this way?
SELECT split('one|||two','[\\|||]')[0]
The "|" is a reserved character, so you need to scape it properly

Hive does have a split() function, but it returns an array. That would be:
select split('one|||two', '[|]{3}')[0]

Related

I am trying to understand how to use the REGEXP_EXTRACT function in BigQuery

I have a column string value:
SELFSERVE TICKET\STATION\TRAINCOMPANY\GBR
I would like to extract the part of the string before the first "\" i.e. SELFSERVE TICKET
What would be the right syntax to achieve this? If there are better functions to use I am ears.
I don't think that a regex is necessary here. You can just use regular string functions:
substr(mycol, 1, strpos(mycol, '\') - 1)
This gives you everything before the first \.
If you have strings that do not contain \ at all, and you want to return the entire string in that case:
substr(concat(mycol, '\'), 1, strpos(mycol, '\') - 1)
Finally, if you want to get all parts at once, you can use split(), which returns an array of strings:
split(mycol, '\')
In BigQuery, I would use:
select split(col, '\')[ordinal(1)] as first_value

Teradata SQL - how to get everything before hyphen?

The question is how to get all data before hyphen using SQL in Teradata?
The pattern is like this : 123ABC-456. I need only 123ABC.
This regex expression:
SELECT RegExp_Replace('123ABC-456', '\w[^-]*$')
returns "123ABC-" with hyphen for some reason
and this "^[^-]*[^ -]" - returns "-456" instead of "123ABC"
Please any help?
You can try using REGEXP_SUBSTR()
select REGEXP_SUBSTR('123ABC-456', '^[^-]*[^ -]')

using length function in REGEXP_REPLACE() in Postgres

I am removing that last 3 characters from the string "ABC123" using regexp_replace function in Oracle using the below statement
select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;
The same result can be achieved in Postgres with the below statements,
select regexp_replace('ABC123','[123]', '','g')
select translate('ABC123','123', '');
Is there any way I can use the length function for replace as I have used in Oracle?
Why not simply use left()?
select left('ABC123', length('ABC123') - 3)
The same idea can be used in Oracle as well, but you need to use the substr() function. This should be more efficient in both databases.
You could also look into the trim functionality.
http://www.postgresqltutorial.com/postgresql-trim-function/
"select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;"
would become
select ltrim('ABC123','ABC') from dual;
resulting in 123

How to select all the string characters preceding a . in oracle

I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.