SQLite select where column does not contain certain data - sql

ok so I have a table that looks something like this...(using pipe symbol to separate columns)
example1 url|0
example2 url|0
example3 url|1
example4 url|1,2
example5 url|1,3,6
What I am trying to do is to select all rows where column 2 does NOT contain a 1.
I cannot use != because if you look all but one of those would return data because the last 2 rows don't equal 1. I tried scouring SQLite documentation for how to go about writing the statement, but I can't find it. This is what I have so far.
select * from table_name where table_column_name[something needed here]'1';

Give that "1,2,3" et al are strings, you probably need the LIKE keyword.
select * from table_name where table_column_name NOT LIKE '%1%'

For matching a list item separated by ,, use:
SELECT * FROM table WHERE `,`||column||`,` NOT LIKE '%,1,%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.

Related

Identify and remove records surrounded in quotes

I have imported a table containing roughly 100,000 records, some of which need to be removed. I'd like to identify and remove any and all records where a particular field (called MyQuery) contains words surrounded in quotation marks, but if there are only TWO quotation marks in the field.
For example I would like to remove
"This is a test"
--but not--
"This is "a" test"
Thank you kindly for any assistance
My apologies to all of you for the terrible structure and clarity of my question. I think I was able to get to the answer based on your suggestions though!
select * from <table> where LEN(myquery) - LEN(REPLACE(myquery,'"','')) = 2 and myquery like '"%"'
Thank you all so much - you're the best!
You can do it like this:
DELETE
-- SELECT * -- To test first!
FROM YourTable
WHERE LEN(MyQuery) - LEN(REPLACE(MyQuery, '"', '')) = 2;
AND MyQuery LIKE '"%"'
You are basically comparing the length if the field after removing all quotes. This is an easy way to determine the number of occurrences of a specific character.
After your comment, I added another the condition so the quotes are always surrounding the field.
You can use the following query to delete all records that contain '"', excluding those that start and end with '"':
DELETE From <TABLENAME> Where COL like '%"%' and (COL not like '"%' Or COL not like '%"')
Or, you can use: Here we delete all records having more than 2 occurrences of "
DELETE FROM <TABLENAME> WHERE (LEN(COL)-len(replace(COL,'"',''))) > 2

Extended Wildcard Search in a Certain Structure of Content

The content of a column contains various entrys. The structure of the contain is as follows:
A
A-B
A-B-C
Y
W-X
C-R-Z
I need a statement to search only for content separated by one dash, as well as a statement to search only for content separated by two dashes.
TIA!
For one dash:
Select * From table where col like '%[a-z]-[a-z]%'
For two dashes:
Select * From table where col like '%[a-z]--[a-z]%'

Find substring in string

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"

Get rows that contain only certain characters

I want to get only those rows that contain ONLY certain characters in a column.
Let's say the column name is DATA.
I want to get all rows where in DATA are ONLY (must have all three conditions!):
Numeric characters (1 2 3 4 5 6 7 8 9 0)
Dash (-)
Comma (,)
For instance:
Value "10,20,20-30,30" IS OK
Value "10,20A,20-30,30Z" IS NOT OK
Value "30" IS NOT OK
Value "AAAA" IS NOT OK
Value "30-" IS NOT OK
Value "30," IS NOT OK
Value "-," IS NOT OK
Try patindex:
select * from(
select '10,20,20-30,30' txt union
select '10,20,20-30,40' txt union
select '10,20A,20-30,30Z' txt
)x
where patindex('%[^0-9,-]%', txt)=0
For you table, try like:
select
DATA
from
YourTable
where
patindex('%[^0-9,-]%', DATA)=0
As per your new edited question, the query should be like:
select
DATA
from
YourTable
where
PATINDEX('%[^0-9,-]%', DATA)=0 and
PATINDEX('%[0-9]%', LEFT(DATA, 1))=1 and
PATINDEX('%[0-9]%', RIGHT(DATA, 1))=1 and
PATINDEX('%[,-][-,]%', DATA)=0
Edit: Your question was edited, so this answer is no longer correct. I won't bother updating it since someone else already has updated theirs. This answer does not fulfil the condition that all three character types must be found.
You can use a LIKE expression for this, although it's slightly convoluted:
where data not like '%[^0123456789,!-]%' escape '!'
Explanation:
[^...] matches any character that is not in the ... part. % matches any number (including zero) of any character. So [^0123456789-,] is the set of characters that you want to disallow.
However: - is a special character inside of [], so we must escape it, which we do by using an escape character, and I've chosen !.
So, you match rows that do not contain (not like) any character that is not in your disallowed set.
Use option with PATINDEX and LIKE logic operator
SELECT *
FROM dbo.test70
WHERE PATINDEX('%[A-Z]%', DATA) = 0
AND PATINDEX('%[0-9]%', DATA) > 0
AND DATA LIKE '%-%'
AND DATA LIKE '%,%'
Demo on SQLFiddle
As already mentioned u can use a LIKE expression but it will only work with some minor modifications, otherwise too many rows will be filtered out.
SELECT * FROM X WHERE T NOT LIKE '%[^0-9!-,]%' ESCAPE '!'
see working example here:
http://sqlfiddle.com/#!3/474f5/6
edit:
to meet all 3 conditions:
SELECT *
FROM X
WHERE T LIKE '%[0-9]%'
AND T LIKE '%-%'
AND T LIKE '%,%'
see: http://sqlfiddle.com/#!3/86328/1
Maybe not the most beautiful but a working solution.

How to do string manipulation in SQL query

I know I'm close to figuring this out but need a little help. What I'm trying to do is all grab a column from a particular table, but chop off the first 4 characters. For example if in a column the value is "KPIT08L", the result I was is 08L. Here is what I have so far but not getting the desired results.
SELECT LEFT(FIELD_NAME, 4)
FROM TABLE_NAME
First up, left will give you the leftmost characters. If you want the characters starting at a specific location, you need to look into mid:
select mid (field_name,5) ...
Secondly, if you value performance,portability and scalability at all, this sort of "sub-column" manipulation should generally be avoided. It's usually far easier (and faster) to patch columns together than to split them apart.
In other words, keep the first four characters in their own column and the rest in a separate column, and do your selects on the relevant one. If you're using anything less than a full column, then it's technically not one attribute of the row.
Try with
SELECT MID(FIELD_NAME, 5) FROM TABLE_NAME
Mid is very powerfull, it let you select the starting point and all the remainder, or,
if specified, the length desidered as in
SELECT MID(FIELD_NAME, 5, 2) FROM TABLE_NAME ' gives 08 in your example text
SELECT RIGHT(FIELD_NAME,LEN(FIELD_NAME)-4)
FROM TABLE_NAME;
If it is for a generic string then the above one will work...
Don't have Access at my current location, but please try this.
SELECT RIGHT(FIELD_NAME, LEN(FIELD_NAME)-4)
FROM TABLE_NAME
The LEFT(FIELD_NAME, 4) will return the first 4 caracters of FIELD_NAME.
What you need to do is :
SELECT MID(FIELD_NAME, 5)
FROM TABLE_NAME
If you have a FIELD_NAME of 10 caracters, the function will return the 6 last caracters (chopping the first 4)!