How to convert this hive query to oracle - oracle

I have this hive query:
select REGEXP_EXTRACT( lower(column_name) , '.*(build[ \t]*(app)?[ \t]*:[ \t]*)(.*?)([ \t]*;[ \t]*essential[ \t]+reason[ \t]+info[ \t]+compilation.*$|$)', 3) from table
How do I convert it to oracle query?
I have tried using regexp_substr() but it doesn't work like it needs to. Thanks!

It would help a lot if you could edit your question to add some example data with your expected output so we could see how it "doesn't work".
regexp_substr() follows the same syntax as regexp_extract(), but Oracle (like many vendors) only supports a specific set of regular expression operators.
In your case, the main issue I see is that \t isn't interpreted as "tab character" in Oracle. You have several alternatives to match tab characters:
with test_data as (select 'build'||chr(9)||':' as s from dual) -- 'build' with a tab character then a ':'
select
regexp_substr(s, 'build[ \t]*:') as slash_t, -- doesn't work
regexp_substr(s, 'build[[:space:]]*:') as posix_cc, -- matches any whitespace characters
regexp_substr(s, 'build\s*:') as perl_cc, -- same as posix, but perl dialect
regexp_substr(s, 'build[ ]*:') as literal_tab, -- StackOverflow formatting ruins this, but there was a tab there
regexp_substr(s, 'build[ '||chr(9)||']*:') as chr_tab -- chr(9) is tab
from test_data;
Output:
SLASH_T POSIX_CC PERL_CC LITERAL_TAB CHR_TAB
------- -------- ------- ----------- -------
build : build : build : build :

Related

keeping & ampersand as a value in a sql where condition with other substitute variable values [duplicate]

I have a SQL script that creates a package with a comment containing an ampersand (&). When I run the script from SQL Plus, I am prompted to enter a substitute value for the string starting with &. How do I disable this feature so that SQL Plus ignores the ampersand?
This may work for you:
set define off
Otherwise the ampersand needs to be at the end of a string,
'StackOverflow &' || ' you'
EDIT: I was click-happy when saving... This was referenced from a blog.
If you sometimes use substitution variables you might not want to turn define off. In these cases you could convert the ampersand from its numeric equivalent as in || Chr(38) || or append it as a single character as in || '&' ||.
I resolved with the code below:
set escape on
and put a \ beside & in the left 'value_\&_intert'
Att
You can set the special character, which is looked for upon execution of a script, to another value by means of using the SET DEFINE <1_CHARACTER>
By default, the DEFINE function itself is on, and it is set to &
It can be turned off - as mentioned already - but it can be avoided as well by means of setting it to a different value. Be very aware of what sign you set it to. In the below example, I've chose the # character, but that choice is just an example.
SQL> select '&var_ampersand #var_hash' from dual;
Enter value for var_ampersand: a value
'AVALUE#VAR_HASH'
-----------------
a value #var_hash
SQL> set define #
SQL> r
1* select '&var_ampersand #var_hash' from dual
Enter value for var_hash: another value
'&VAR_AMPERSANDANOTHERVALUE'
----------------------------
&var_ampersand another value
SQL>
set define off <- This is the best solution I found
I also tried...
set define }
I was able to insert several records containing ampersand characters '&' but I cannot use the '}' character into the text
So I decided to use "set define off" and everything works as it should.
According to this nice FAQ there are a couple solutions.
You might also be able to escape the ampersand with the backslash character \ if you can modify the comment.
I had a CASE statement with WHEN column = 'sometext & more text' THEN ....
I replaced it with
WHEN column = 'sometext ' || CHR(38) || ' more text' THEN ...
you could also use
WHEN column LIKE 'sometext _ more text' THEN ...
(_ is the wildcard for a single character)

Replace text starting with & symbom Oracle [duplicate]

I have a SQL script that creates a package with a comment containing an ampersand (&). When I run the script from SQL Plus, I am prompted to enter a substitute value for the string starting with &. How do I disable this feature so that SQL Plus ignores the ampersand?
This may work for you:
set define off
Otherwise the ampersand needs to be at the end of a string,
'StackOverflow &' || ' you'
EDIT: I was click-happy when saving... This was referenced from a blog.
If you sometimes use substitution variables you might not want to turn define off. In these cases you could convert the ampersand from its numeric equivalent as in || Chr(38) || or append it as a single character as in || '&' ||.
I resolved with the code below:
set escape on
and put a \ beside & in the left 'value_\&_intert'
Att
You can set the special character, which is looked for upon execution of a script, to another value by means of using the SET DEFINE <1_CHARACTER>
By default, the DEFINE function itself is on, and it is set to &
It can be turned off - as mentioned already - but it can be avoided as well by means of setting it to a different value. Be very aware of what sign you set it to. In the below example, I've chose the # character, but that choice is just an example.
SQL> select '&var_ampersand #var_hash' from dual;
Enter value for var_ampersand: a value
'AVALUE#VAR_HASH'
-----------------
a value #var_hash
SQL> set define #
SQL> r
1* select '&var_ampersand #var_hash' from dual
Enter value for var_hash: another value
'&VAR_AMPERSANDANOTHERVALUE'
----------------------------
&var_ampersand another value
SQL>
set define off <- This is the best solution I found
I also tried...
set define }
I was able to insert several records containing ampersand characters '&' but I cannot use the '}' character into the text
So I decided to use "set define off" and everything works as it should.
According to this nice FAQ there are a couple solutions.
You might also be able to escape the ampersand with the backslash character \ if you can modify the comment.
I had a CASE statement with WHEN column = 'sometext & more text' THEN ....
I replaced it with
WHEN column = 'sometext ' || CHR(38) || ' more text' THEN ...
you could also use
WHEN column LIKE 'sometext _ more text' THEN ...
(_ is the wildcard for a single character)

Underscore is not working in oracle like clause

When development, I used 'test_1%' to find 'test_123' in like. But in production environment its not working. Using 'escape '\'' is working. is there any setting needs to set in oracle? I want to use without escape '\''.
try this in SQL Developer:
SELECT * FROM TABLE1 WHERE NAME LIKE 'test\_1%' escape '\'
in sql plus:
set escape '\'
SELECT * FROM TABLE1 WHERE NAME LIKE 'test\_1%';
In Oracle, you can also use ESCAPE like this:
SELECT * FROM name_of_table WHERE description LIKE 'testing\_%' ESCAPE '\';
The other answers using the ESCAPE '\' didn't work for me, but I was able to overcome this issue by using a REPLACE function:
SELECT * FROM name_of_table WHERE REPLACE(description, '_', '~') LIKE 'testing~%';
For me this has worked (using ^ as escape character):
select * from all_tables where table_name not like '%^_%' escape '^' ;
Using _ as escape character and using double _ in like condition has worked for me.
If you want to add more condition, put ESCAPE keyword after LIKE condition.
SELECT * FROM USER_TABLES WHERE TABLE_NAME LIKE '%__%' ESCAPE '_' AND ...

Oracle SELECT INTO OUTFILE, what's wrong with this query?

This is the query that I am executing from sqlplus:
select * into outfile 'my_file.txt'
fields terminated by '\t' lines terminated by '\n'
from my_table where my_column = 'stuff';
I get the following error:
FROM keyword not found where expected
What am I doing wrong?
P.S. I know that there are other ways to flush the output to file but I really want to win this against Oracle...
SELECT ... INTO OUTFILE is MySQL-specific syntax. It won't work on other DBMSs such as Oracle.
In Oracle you would surround the statement with SPOOL filename...SPOOL OFF.

Incorrect syntax near ')'

I have a BasicMSI project (Installshield 2009) that runs a SQL script during the installation process.
During the installation I receive the following error.
Error 27506.Error executing SQL script {SCRIPTNAME}. Line 352. Incorrect syntax near ')'. (102)
The problem is that I don't have any ')' at line 352 of the script and also the script works without any problems if I run it with SQL Management Studio Express.
Can anyone tell me what is the problem and how can I fix it?
Thanks.
PS. I cannot set the script error handling option to "On Error, Goto Next Statement" because therefor it will not create some of my foreign keys.
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRIGGER_NAME]'))
EXEC dbo.sp_executesql #statement = N'
CREATE TRIGGER [dbo].[TRIGGER_NAME]
ON [dbo].[TABLE_NAME] -- LINE: 352
INSTEAD OF INSERT
AS
BEGIN
DECLARE #Count INT;
SET #Count = (SELECT COUNT([Name])
FROM TABLE_NAME
WHERE IsDeleted = 0 AND [Name] IN (SELECT [Name] FROM INSERTED));
IF #Count > 0
BEGIN
RAISERROR (''Error Message.'', 16, 1);
Rollback;
END
ELSE
BEGIN
INSERT INTO dbo.TABLE_NAME SELECT {Columns} FROM INSERTED;
SELECT CONVERT(BigInt,SCOPE_IDENTITY()) AS [value]
END
END
'
GO
I was getting similar errors (one with ')' as the offending character, one with ';' as the offending character). Then I noticed that when InstallShield imported my scripts, it had changed ">" to ">" and "<" to "<" and "&" to "&". Doing search-and-replace across the imported scripts in the InstallShield script editor for these three substitutions fixed the issue for me.
It seems reasonable that this error might occur if you've written an IN statement, which you populate programmatically, only at runtime some values are missing, resulting in a statement saying "... WHERE x IN()", which is invalid.
This would generate that error, and also, it is an error that could easily appear in one environment but not another. It is hard to give more detail than that without actually seeing the script.

Resources