I have this like select and its working fine:
select * from printers where
printer_id like 'XERX-15%' Or printer_id like 'OFFJT-16%' ;
I am trying to convert the expression to regexp_like because I have many of these like conditions,
select * from printers where
regexp_like(printer_id,'XERX-15%|OFFJT-16%')
seems like its giving wrong results...
create table printers(printer_id varchar(50));
insert into printers values('XERX-1500');
insert into printers values('XERX-1550');
insert into printers values('XERX-1560');
insert into printers values('XERX-1570');
insert into printers values('XERX-1601');
insert into printers values('XERX-1601');
insert into printers values('XERX-1602');
insert into printers values('XERX-1603');
insert into printers values('OFFJT-1504');
insert into printers values('OFFJT-1604');
select * from printers where
printer_id like 'XERX-15%' Or printer_id like 'OFFJT-16%' ;
select * from printers where
regexp_like(printer_id,'XERX-15%|OFFJT-16%')
http://sqlfiddle.com/#!4/44c23
% is not a wild-card in regular expressions.
You want:
select *
from printers
where regexp_like(printer_id,'^(XERX-15|OFFJT-16)')
^ matches the start of the string then
either XERX-15 or OFFJT-16.
If you don't want to use a capturing group () then you could equivalently use:
select *
from printers
where regexp_like(printer_id,'^XERX-15|^OFFJT-16')
The % symbol is a like wildcard. In your regex it's a literal character; so remove it:
regexp_like(printer_id,'XERX-15|OFFJT-16')
Or to only match at the start of the line (as your original does):
regexp_like(printer_id,'^(XERX-15|OFFJT-16)')
SQL fiddle
Related
I'm connected to a SQLite database named db.sqlite3 on Windows. After running .tables command I know which tables I have, so I'm trying to show one of them with select * from table1, without seeing any output in Windows Terminal.
Why is this happening? Should I use some special command to print the query output in terminal?
I suspect your table has zero rows in it. Consider the following example in a blank database:
-- create a table
sqlite> create table "test"("field1" INTEGER);
-- select rows
sqlite> select * from test;
-- note: no output!
-- insert some values
sqlite> insert into test values(1),(2);
-- try the select again
sqlite> select * from test;
1
2
-- we have output!
-- to check if you have rows, do this:
sqlite> select count(*) from test;
2
-- 2 rows present
I am learning how to use FTS4 in SQlite and trying to translate something like this:
SELECT * FROM table
WHERE (postal_code LIKE q1 OR place_name LIKE q1) AND (address LIKE q2)
which works perfectly with the normal table, to something like that:
SELECT * FROM table
WHERE table MATCH '(postal_code: q1 OR place_name: q1) AND (address: q2)'
which is not working with the same table converted to VIRTUAL .. USING fts4...
Searching for match in specific column with ":" is working, but how can I combine this with OR and AND?
Background - I try to work with a flexible query, where q1 can be one of the both: postal-code or place name and q2 has to be address.
EDIT:
This is what I got so far (partially) working
SELECT * FROM table WHERE table MATCH ('postal_code:q1 OR place_name:q1')
INTERSECT
SELECT * FROM table WHERE table MATCH ('address:q2');
(building on this post from #CL)
The first problem here are words with space - if I search for something like
...MATCH ('postal_code:"new haven*" OR place_name:"new_haven*"')
then I have "malformed MATCH expression"
The second problem is when I have empty query, like
...MATCH ('*')
then I get nothing
How can I search for a name like O'Neil from a table when I use a query like
select * from table_name where name like 'O'Neil';
then it shows an error.
Escape it with the second single quote:
select * from table_name where name like 'O''Neil';
Since Oracle 10g there is also a quote-operator:
select * from table_name where name like q'('O'Neil)';
Syntax: q'c text-to-be-quoted c'. c is a single character (called the quote delimiter). With the «quote operator» apostrophes don't have to be doubled.
I have a SQLITE3 database wherein I have stored various columns. One column (cmd) in particular contains the full command line and associated parameters. Is there a way to extract just the first word in this column (just before the first space)? I am not interested in seeing the various parameters used, but do want to see the command issued.
Here's an example:
select cmd from log2 limit 3;
user-sync //depot/PATH/interface.h
user-info
user-changes -s submitted //depot/PATH/build/...#2011/12/06:18:31:10,#2012/01/18:00:05:55
From the result above, I'd like to use an inline SQL function (if available in SQLITE3) to parse on the first instance of space, and perhaps use a left function call (I know this is not available in SQLITE3) to return just the "user-sync" string. Same for "user-info" and "user-changes".
Any ideas?
Thanks.
My soluion:
sqlite> CREATE TABLE command (cmd TEXT);
sqlite> INSERT INTO command (cmd) VALUES ('ls'),('cd ~'),(' mpv movie.mkv ');
sqlite> SELECT substr(trim(cmd),1,instr(trim(cmd)||' ',' ')-1) FROM command;
ls
cd
mpv
Pros:
it's not that a dirty hack
it only uses core functions
"Finds the first occurrence" function is one of the SQLite3 Core Functions (http://www.sqlite.org/lang_corefunc.html).
Of course, it is much better to use instr(X,Y).
So you can write:
SELECT substr(cmd,1,instr(cmd,' ')-1) FROM log2
As the position of your first space character is unknown, I don't think there is a corefunction in SQLite that will help.
I think you'll have to create one http://www.sqlite.org/c3ref/create_function.html
Here's a hack
sqlite> create table test (a);
sqlite> insert into test values ("This is a test.");
sqlite> select * from test;
This is a test.
sqlite> select rtrim(substr(replace(a,' ','----------------------------------------------------------------------------------------'),1,80),'-') from test;
This
It works as long as your longest command is less than 80 characters (and you include 80 '-' characters in the substitution string -- I didn't count them!). If your commands can contain '-' just use a different character that is not allowed in the commands.
I don't believe that's something you'll be able to do within the SQL itself. SQLite's support for string handling functions is not as extensive as other RDBMSs (some of which would let you do a SUBSTR with a Reg Exp).
My suggestion is either to write your own SQL function as suggested by #Jon or just do it as a post-processing step in your app code.
In an SQL table I have following columns
id, title, content
And I would like to search text inside title or content having "[" or "]".
What will be the SQL command to fetch such kind of data?
(edit: removed "efficient")
(added)
As per "rsbarro"
I have tried and my findings are:
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%[%'
returns 0 row.
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%]%'
returns number of rows.
Try using the LIKE operator, but remember if you are searching for a [ you need to escape it as [[] (see #billinkc's answer for an explanation on the escaping, or check the MSDN documentation and look for the section "Using Wildcard Characters As Literals"). The statement:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%'
will search for any record in MyTable where Title contains a [. The % is a wildcard character.
I'm not exactly sure from the question what you are trying to find, but if you want to search for a title containing both brackets (i.e., '??[match]??'), try:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%]%'
EDIT
If you are trying to match either [ or ] in title or in content, the query would be:
SELECT * FROM MyTable
WHERE
Content LIKE '%[[]%' OR Content LIKE '%]%' OR
Title LIKE '%[[]%' OR Title LIKE '%]%'
This query would probably not be terribly efficient if you have a table with a large number of rows.
The examples above are failing for like matching because LIKE offers a single character match option with [].
Any single character within the specified range ([a-f]) or set ([abcdef]).
To actually match a [, you will need to embed it in a pair of brackets
;
WITH DEMO (Title) AS
(
SELECT 'Where are you [ I am here]'
UNION ALL SELECT 'WHO ARE YOU'
UNION ALL SELECT 'asdafasfa][asdfadfasdfasdfa'
)
SELECT
D.*
FROM
DEMO D
WHERE
D.Title LIKE '%[[]%'
OR D.Title LIKE '%[]]%'
Returns
Where are you [ I am here]
asdafasfa][asdfadfasdfasdfa
In Sql [ is a special character and where [[] means [, so the below Sql query will work accordingly.
SELECT * FROM MyTable WHERE (Title LIKE '%[[]%' OR content LIKE '%[[]%')
OR (Title LIKE '%]%' OR content LIKE '%]%')
Please see herefor more details.