SQLite - display table names finishing by "_1" - regex

I am looking for a way to display table names I have in a database for which the name is ending by "_1".
I tried to use the command:
.tables '%_1';
Unfortunately the underscore symbol is used in the expression matching, so it returned me tables such as:
"125_1","125_11","125_21".
Only the first one is interesting in this example, I will not display the full result because there are hundreds of tables. So I tried something like this:
.tables '%_1' ESCAPE '_';
And it gave me the exact same result.
If you have a solution to overcome the problem of the underscore symbol, please post it.
remember that I have hundreds of tables with names following this pattern in regex: "^\d+_\d+$"

This is not how the ESCAPE clause works. To search for an underscore, you must escape the underscore with the escape character:
LIKE '%#_1' ESCAPE '#'
Anyway, .tables is not an SQL command and ignores the ESCAPE clause. To do your own search, you have to run your own query:
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name LIKE '%#_1' ESCAPE '#';

Related

Is there any way to write a HQL query which needs to be executed through shell script where the task is to remove characters except A-Z0-9a-z,&-“#/;:

I wish to write a HQL query which is executed through shell script.
The main purpose of the query is the data should not contain any characters other than A-Z, 0-9, space, and special characters other than comma, ampersand, dash, # # / semicolon, colon.
Please find the query which i used to achieve my requirement although i was not 100% successful.
select REGEXP_REPLACE(REGEXP_REPLACE(Name,'[^a-zA-Z \\-\\\']',''),'^[\\-\\\' ]*','')
from source_table
Please find the samples i used to test:
'Proa$$%%'
'$%$%&'
'$*&%$'
',&-“#/;:$'
'XYZ.'
Expected output is :
'Proa'
'&'
'&'
',&-“#/;:’'
'XYZ'
Please help me in solving this requirement
Try with this (\\$|\\.|\\%|\\*) regex.
Example:
hive> select regexp_replace(name,"(\\$|\\.|\\%|\\*)","") from source_table;
Output:
Proa
&
&
,&-“#/;:
XYZ
The above query you can keep in shell script using hive -e.
hive -e 'select regexp_replace(name,"(\\$|\\.|\\%|\\*)","") from <db_name>.source_table'
Update:
select regexp_replace(name,'[^a-zA-Z0-9\\,\\:\\;\\&\\-\\#\\/]','') from <db_name>.source_table;
Output:
Proa
&
&
,&-#/;:
XYZ
Thanks 484.
It's working fine.
Only change i made to my script is instead of \ i added two more.Please find the below for the same.
select regexp_replace(name,'[^a-zA-Z0-9\\\\,\\\\:\\\\;\\\\&\\\\-\\\\#\\\\/]','') from <db_name>.source_table;
the above code works fine in my shell script.
Issue is resolved.

Select field with Hyphen in Redshift Spectrum

I am trying to extract a nested field with an Hyphen in the name through Redshift Spectrum
SELECT mystruct.mysubstruct.my-field.id
FROM my_external_schema.my_table
I see in other DBMS is suggested to wrap the field name with double quotes:
"mystruct.mysubstruct.my-field.id"
or back ticks
`mystruct.mysubstruct.my-field.id`
but none of these worked for me.
Any suggesitons?
Since the double quotes permit to escape the special characters, doing "mystruct.mysubstruct.my-field.id" means that you are looking for the column named 'mystruct.mysubstruct.my-field.id' at top level and not as the nested column, because the dot is not used to extract the field.
What you have to do is
SELECT mystruct.mysubstruct."my-field".id
FROM my_external_schema.my_table

Regex query to remove character(s) from lines that has a phrase

I'm trying to convert my DDL's from Oracle to Postgres but I'm having a problem with double quote characters. I want to remove double quotes from each and every line which contains "CREATE TABLE " phrase. For example I want this: CREATE TABLE "ILIKEMEMES" to be converted to this: CREATE TABLE ILIKEMEMES but I don't want line ("ID" VARCHAR(255) to change either. I'm doing this on Notepad++ so Python scripts wouldn't be my first choice of solution.
Try doing the following find and replace, in regex mode:
Find: \bCREATE TABLE "(.*?)"
Replace: CREATE TABLE $1
This will target only create table statements having a table name which appears in double quotes.

Regexp_like vs regex validators online - diferent results

I have a regex expression for email validation using plsql that is giving me some headaches... :)
This is the condition I'm using for an email (rercear12345#gmail.com) validation:
IF NOT REGEXP_LIKE (user_email, '^([\w\-\.]+)#((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$') THEN
control := FALSE;
dbms_output.put_line('EMAIL '||C.user_email||' not according to regex');
END IF;
If I make a select based on the expression I don't get any values either:
Select * from TABLE_X where REGEXP_LIKE (user_email, '^([\w\-\.]+)#((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$');
Using regex101.com I get full match with this email: rercear12345#gmail.com
Any idea?
The regular expression syntax that Oracle supports is in the documentation.
It seems Oracle doesn't understand the \w inside the []. You can expand that to:
with table_x (user_email) as (
select 'rercear12345#gmail.com' from dual
union all
select 'bad name#gmail.com' from dual
)
Select * from TABLE_X
where REGEXP_LIKE (user_email, '^[a-zA-Z_0-9.-]+#((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|([a-zA-Z_0-9-]+.)+[a-zA-Z]{2,4})$');
USER_EMAIL
----------------------
rercear12345#gmail.com
You don't need to escape the . or - inside the square brackets, by doing that you would allow literal backslashes to be matched.
This sort of requirement has come up before - e.g. here - but you seem be allowing IP address octets instead of FQDNs, enclosed in literal square brackets, which is unusual.
As #BobJarvis said you could also use the [:alnum:] but would still need to include underscore. That could allow non-ASCII 'letter' characters you aren't expecting; though they may be valid, as are other symbols you exclude; you seem to be following the 'common advice' mentioned in that article though.

Wrap JSON field names in quotes using vim search and replace

I have a simple json file that isn't well formatted it looks like:
{ ID: '092558667',
NAME: 'Store Made',
PARENT_CATEGORY_ID: '692558669',
INCLUDED_IN_NET_SALES: '1' }
All I need to do is wrap the field names in double quotes. In vim the closest I have gotten is to wrap the field name and the colon in quotes - obviously I need to figure out how to get the string without the colon wrapped. Here's what I am trying:
:%s/[A-Z_]*:/"&"
If I leave the colon out of the query the whole file ends up being selected.
You can use capture groups:
%s/\([A-Z_]*\):/"\1":/
To handle already quoted keys properly:
%s/"\?\([A-Z_]*\)"\?:/"\1":/
Ok, with the information above I ended up with this:
:%s/[ \t]\([A-Za-z_].*\):/"\1":/
it supports upper- and lowercase chars
it skips already quoted fields
Since this can be considered a completion, I mapped it to a vim completion shortcut ctrl-x ctrl-j in .vimrc (they all start with ctrl-x ) :
:noremap <C-x><C-j> :%s/[ \t]\([A-Za-z_].*\):/"\1":/<CR>