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.
Related
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
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 '#';
I have a sql procedure code. We are migrating the code on different schema. I need to replace all the dimension tables schema.
Example:
Old schemas: DBO.ABC_DIM, DBO.XYZ_DIM
After replace: MART.ABC_DIM, MART.XYZ_DIM
Could any one let me know how we can do this using regex replace.
Thanks
Sky
You must use:
in the "Find what" field:
(DBO)\.
and in the "Replace with" field:
MART\.
Don't forget to place the cursor at beginning of the file. Otherwise the replacements begin after actually cursor position
EDITED:
So in this case if you have others, you can use that:
Find field:
\b(DBO\.)(.+?)_DIM\b
Replace field:
MART\.$2_DIM
Some like:
DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD
become:
MART.ABC_DIM, MART.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD
LAST EDIT:
The above fail with:
DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD, DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD,
DBO.ABC_DIM, DBO.XYZ_DIM,
Because in the second row match DBO.ABC_DTL, DBO.ABC_2_BCD, DBO.ABC_DIM
And DBO.ABC_DTL become MART.ABC_DTL
So the right solution is:
Find field:
(DBO\.)(.[^\.]+?)_DIM
Replace field:
MART\.$2_DIM
see matching results here: http://refiddle.com/refiddles/596b348175622d74ff020000
if you open that schema in VIM, do press esc and then
:s%/DBO/MART
and press enter
:s (colon and s) for substitute
/DBO find DBO
/MART replace it with MART
once you verify that all the DBOs are replace with MART, you need to save the changes by esc and :wq
I have a set of SQL script that wants to change schema.
create table Service.Table1 (col1 varchar(100));
create table Operation.Table2 (col1 varchar(100));
create table Support.Table3 (col1 varchar(100));
However, the schema is going to change
Service -> Sev
Operation -> Opn
Support -> Spt
The search regular expression is easy ([A-Za-z0-9_]+)\.([A-Za-z0-9_]+)
However, how to do the conditional replacement in Notepad++ or other tools if they can?
Thanks!
If you have a predefined set of the schemas, you may use the conditional replacement in Notepad++ like this:
Find: (?:(?<a>Service)|(?<b>Operation)|(?<c>Support))\.(?<n>[A-Z0-9_]+)
Replace: (?{a}Sev:(?{b}Opn:Spt)).$+{n}
Match Case must be ticked off, and Regular expression must be on.
I would run replace 3 times, once for each schema name:
Find:
create table Service\.
Replace with:
create table Svc.
Find:
create table Support\.
Replace with:
create table Spt.
Find:
create table Operation\.
Replace with:
create table Opn.
Or here is one that uses groups references:
Find:
Service(\.[^\s]+)(.*)
Replace with:
Svc\1\2
Here \1 will hold the dot operator and the table name and \2 holds the rest of the line.
Notepad++ regex implementation is not really powerfull; so,
other tools if they can?
Here is a way to do it:
perl -pi.back -e '%tr=(Service=>"Sev",Operation=>"Opn",Support=>"Spt");s/(?<=create table )(\w+)/$tr{$1}/e;' TheFile
You can add any number of Original => 'Modified' as you want within the hash %tr.
TheFile will be backuped into TheFile.back before processing.
I'm converting a sqlite3 database to mysql.
I have a nice command file for sed that changes AUTOINCREMEMT and the other things needed, but I'm stuck on the last one: double quotes.
sqlite3 dump format:
CREATE TABLE "products" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar(255),
"desc" varchar(255) );
INSERT INTO "products" VALUES(1,'Flux Capacitor',
'This is the "real" thing.\nPromise!')
For the first statement, I can replace all double quotes with backticks and mysql will be happy.
However, my product information has double quotes in the data. How can I exclude these from being replaced? I was trying to replace only those double quotes with a placeholder, then I could replace all the other double quotes, then I could change the placeholder back, but my regex-fu isn't up to par.
This was as far as I got:
/"[^"]*"/s
... to match the double quoted texts, but I couldn't figure out how to qualify that only double quotes inside single quotes should be matched.
I would change MySQL to accept double-quotes as identifier delimiters. This is standard SQL behavior, and you can make MySQL behave that way with a SQL mode:
mysql> SET SQL_MODE = ANSI;
Or more specifically:
mysql> SET SQL_MODE = ANSI_QUOTES;
Then MySQL should understand your data dump.
See "Server SQL Modes" for more information.
Well I know how to easily solve it in PHP with preg_replace_callback():
<?php
$sql = file_get_contents('sqlite3 dump.txt');
function callback($match) { return str_replace('"', '`', $match[0]); }
$sql = preg_replace_callback('/CREATE TABLE .*?;/s', callback, $sql);
echo preg_replace_callback('/INSERT INTO .*? VALUES/s', callback, $sql);
?>
Unless you can "SET SQL_MODE = ANSI_QUOTES" as Bill Karwin said.
I can replace all double quotes with backticks and mysql will be happy.
Happy for now, but it wouldn't have solved the whole problem, so could easily fall over in the future. Apostrophe and backslash also work differently in MySQL.
my product information has double quotes in the data. How can I exclude these from being replaced?
You can't reliably. SQL syntax is actually quite complex, and cannot in the general case be parsed by regex hacking.
Bill's suggestion with changing SQL_MODE to fit the existing syntax is a much better approach. I run MySQL in ANSI mode all the time, as I dislike having to tailor my apps to one particular database's foibles.