I have an sql dump of different tables each with different amount of fields, I want to insert a query after each one, so I'm trying to find a regex statment that would retreive:
CREATE TABLE cms_audit (
aud_id bigint NOT NULL IDENTITY(1,1),
user_id int DEFAULT NULL,
client_id int NOT NULL,
aud_event varchar(500) NOT NULL,
aud_type varchar(150) NOT NULL,
aud_string varchar(1000) DEFAULT NULL,
aud_date datetime DEFAULT NULL
)
-- --------------------------------------------------------
My regex is CREATE TABLE .*-- (in notepad++ I've checked the box that say's . matches newline) in my head this means get all that starts with "create table" and whatever is after it until you reach "--".
However this statement is retrieving the entire file instead of getting each "create table" query separately, what am I missing?.
I have also tried CREATE TABLE (.*|\n)*--.. didn't work.
You need to use a regex with any character except --. To achieve this you can do:
CREATE TABLE (?:(?!--).)*
EDIT
The ?! is to make a Negative Lookahead of the string --. Nothing with this string will match this expression.
You can see and test it with this link (it's very well explained and a good tool):
https://regex101.com/r/mR9fD4/1
Related
I am trying to add a string '_$' to a index name and a table name as follows. I need to use a method 'regexp_replace' in SELECT statement.
select regexp_replace(input_string......)
# Input
CREATE UNIQUE INDEX "SCOTT"."PK_EMP" ON "SCOTT"."EMP" ("EMP_NO")
# Desired Output
CREATE UNIQUE INDEX "SCOTT"."PK_EMP_$" ON "SCOTT"."EMP_$" ("EMP_NO")
Can you help me to build a regular expression for that?
Fairly brute solution would be using the following pattern:
(.*)(" ON ".*)(" \(.*)
with the following replace string:
\1_$\2_$\3
The pattern works by splitting the input in the places where you need to insert the _$ token, and then joining it back placing the tokens in the places we split the input:
CREATE UNIQUE INDEX "SCOTT"."PK_EMP|" ON "SCOTT"."EMP|" ("EMP_NO")
Full SELECT query would look like that:
SELECT REGEXP_REPLACE(
'CREATE UNIQUE INDEX "SCOTT"."PK_EMP" ON "SCOTT"."EMP" ("EMP_NO")',
'(.*)(" ON ".*)(" \(.*)',
'\1_$\2_$\3'
) RX
FROM dual;
I used DB Browser for SQLite version 3.6.0; SQLite Version 3.8.9.
This application already supports Regular Expression out of box (sqlitebrowser). I can use regexp on column brand but failed on column revision;
For example
SELECT brand,revision FROM TDevice where TDevice.brand regexp '^ASUS$'
and the result is 114 Rows returned from: SELECT brand,revision FROM TDevice WHERE TDevice.brand regexp '^ASUS$'; (took 51ms)
However, if regexp is applied on different column, then I get the error
SELECT brand,revision FROM TDevice WHERE TDevice.revision regexp '^ASUS$';
and the error message is invalid operand: SELECT brand,revision FROM TDevice WHERE TDevice.revision regexp '^ASUS$';
Both brand and revision are of TEXT type. The table creation schema is as below:
CREATE TABLE `TDevice` (
`id` INTEGER NOT NULL,
`brand` varchar(128) NOT NULL,
`model` varchar(128) NOT NULL,
`revision` TEXT,
PRIMARY KEY(id)
);
Both brand and revision are of TEXT type. The table creation schema is as below:
No They are different see your table description correctly if you change the TEXT to varchar it will work fine.
Or I will check and inform you how to use regex or can we use regex with TEXT datatype.
or you can convert(CAST) your TEXT to varchar and can perform the match operations
See this post for how to CAST TEXT into varchar
Need to convert Text field to Varchar temporarily so that I can pass to a stored procedure
The difference between brand and revision is that brand cannot accept NULL text. After I fill the revision with empty string '':
UPDATE TDevice SET revision='' WHERE revision IS NULL
, this invalid operand error is resolved.
So last week I was able to begin to stream my Appengine logs into BigQuery and am now attempting to pull some data out of the log entries into a table.
The data in protoPayload.resource is the page requested with the querystring paramters included.
The contents of protoPayload.resource looks like the following examples:
/service.html?device_ID=123456
/service.html?v=2&device_ID=78ec9b4a56
I am getting close, but when there is another entry before device_ID, I am not getting it. As you can see I am not great with Regex, but it is the only way I think I can parse the data in the query. To get just the device ID from the first example, I was able to use the following example. Works great. My next challenge is to the data when the second parameter exists. The device IDs can vary in length from about 10 to 26 characters.
SELECT
RIGHT(Regexp_extract(protoPayload.resource,r'[\?&]([^&]+)'),
length(Regexp_extract(protoPayload.resource,r'[\?&]([^&]+)'))-10) as Device_ID
FROM logs
What I would like is just the values from the querystring device_ID such as:
123456
78ec9b4a56
Assuming you have just 1 query string per record then you can do this:
SELECT REGEXP_EXTRACT(protoPayload.resource, r'device_ID=(.*)$') as device_id FROM mytable
The part within the parentheses will be captured and returned in the result.
If device_ID isn't guaranteed to be the last parameter in the string, then use something like this:
SELECT REGEXP_EXTRACT(protoPayload.resource, r'device_ID=([^\&]*)') as device_id FROM mytable
One approach is to split protoPayload.resource into multiple service entries, and then apply regexp - this way it will support arbitrary number of device_id, i.e.
select regexp_extract(service_entry, r'device_ID=(.*$)') from
(select split(protoPayload.resource, ' ') service_entry from
(select
'/service.html?device_ID=123456 /service.html?v=2&device_ID=78ec9b4a56'
as protoPayload.resource))
I have a table with one column having a large json object in the format below. The column datatype is VARCHAR
column1
--------
{"key":"value",....}
I'm interested in the first value of the column data
in regex I can do it by .*?:(.*),.* with group(1) giving me the value
How can i use it in the select query
Don't do that, it's bad database design. Shred the keys and values to their own table as columns, or use the XML data type. XML would work fine because you can index the structure well, and you can use XPATH queries on the data. XPATH supports regexp natively.
You can use regular expression with xQuery, you just need to call the function matches from a SQL query or a FLORW query.
This is an example of how to use regular expressions from SQL:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
http://angocadb2.blogspot.fr/2014/04/regular-expressions-in-db2.html
DB2 doesn't have any built in regex functionality, unfortunately. I did find an article about how to add this with libraries:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
Without regexes, this operation would be a mess. You could make a function that goes through the string character by character to find the first value. Or, if you will need to do more than this one operation, you could make a procedure that parses the json and throws it into a table of keys/values. Neither one sounds fun, though.
In DB2 for z/OS you will have to pass the variable to XMLQUERY with the PASSING option
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')'
PASSING t.text as "TEXT") as integer) = 0
)
select * from val"
in sqlite it is possible to have string by which the table was created:
select sql from sqlite_master where type='table' and tbl_name='MyTable'
this could give:
CREATE TABLE "MyTable" (`id` PRIMARY KEY NOT NULL, [col1] NOT NULL,
"another_col" UNIQUE, '`and`,''another'',"one"' INTEGER, and_so_on);
Now I need to extract with this string any additional parameters that given column name has been set with.
But this is very difficult since the column name could be enclosed with special characters, or put plain, column name may have some special characters that are used as encapsulation etc.
I don't know how to approach it. The result should be having a column name the function should return anything that is after this name and before , so giving it id it should return PRIMARY KEY NOT NULL.
Use the pragma table_info:
http://www.sqlite.org/pragma.html#pragma_table_info
sqlite> pragma table_info(MyTable);
cid|name|type|notnull|dflt_value|pk
0|id||1||1
1|col1||1||0
2|another_col||0||0
3|`and`,'another',"one"|INTEGER|0||0
4|and_so_on||0||0