SQL Query - How to not include some results - sql

I apologize if this question was asked before I just couldn't correctly formalize it. I have a code column in a table and want to query it but remove some elements with some particular code. Say I want to take elements with code starting from 4 but not include the elements with code whose 6-th number is 9 (1121290).
The code column contains string of numbers with max-length of 8 char. and I want to take almost everything that starts with 4 except elements that start with 411, 427 and 428

Yes you can give query like this:--
I have column element with 6 digit codes.
I am fetching element whose 1st digit is 4 or 6th is 9.
we have to use % and _ for fetching..
SELECT element FROM "table" WHERE element LIKE '%4____9%';
try this it will work.
Everything that starts with 4 except elements that start with 411, 427 and 428 :-
SELECT element FROM "table" WHERE element LIKE '%4__';
1st digit is 4 and 6th is not 9: I tested this one it is working fine try this :-
SELECT element
FROM "table"
WHERE element NOT LIKE '%_____9' and element LIKE '%4_____'

It might be easiest to simply spell out each condition in the where clause:
WHERE code like '4____9%' AND code NOT LIKE '411%' AND code NOT LIKE '427%' AND code NOT LIKE '428%'
The extra conditions won't hurt the query's efficiency much; it's going to have to scan every single row starting with 4 anyway.

Assuming that your "CODE" field is varchar and you are using SQL Server,
you can use the below Query
select * from yourTable
where code like '4%'
AND CHARINDEX('9',code)<>6

Here is a more compact version.
`SELECT * FROM table_name WHERE code IN ('4%') and code NOT IN ('411%','427%', '428%','_____9%');`

Related

select row based on what a substring in a column might contain

I'm looking to select the primary key of a row and I've only got a column that contains info (in a substring) that I need to select the row.
E.g. MyTable
ID | Label
------------
11 | 1593:#:#:RE: test
12 | 1239#:#:#some more random text
13 | 12415#:#:#some more random text about the weather
14 | 369#:#:#some more random text about the StackOverflow
The label column has always a delimiter of :#:#:
So really I guess, I'd need to be able to split this row by the delimiter, grab the first part of the label column (i.e. the number I'm looking) to get the id I wanted.
So, If I wanted row with ID of 14, then I'd be:
Select ID from MyTable
where *something* = '369'
Any ideas on how to construct something ..or how best to go about this:)
I'm completely stumped and haven't been able to find how to do this.
Thanks,
How about:
WHERE label LIKE '369#%'?
No reason to get fancy.
Although.. if you are going to do this search often, then maybe pre-split that value out to another column as part of your ETL process and index it.

Access Update Query First 5 digits, but last number goes up one

I am using Access Update Query to change a column to the first 5 digits, I got that part. But I ALSO need the last digit to go up one. So if its 12345 I need it to be 12346.
This is what I have so far:
Left([Num],5)
Try this:
CLng(Left([Num], 5)) + 1
The CLng is only necessary if the original column isn't already a number field.

Select statement with offset in like

I have an entry "123456789" in my table.
Select * from map where col like '%1%5%6%7%9'.
I want to retrieve the records where the order of the input sequence matches, but i also want to ensure that the distance between any 2 matching digits is less than 2.
Is there any way i can specify an offset ?
The input is 189, and it selects the record, but i want 1.8.9 to be within 2 of each other. 12879 would be an acceptable output but 123456789 would not be.
Below statement requires 3 to 5 characters between 1 and 5:
SELECT * FROM map WHERE col LIKE '%1___%5%6%7%9' AND col NOT LIKE '%1______%5%6%7%9%'
Using _s you may force any count of characters.
EDIT: Character corrected. Source: SQLite expression
Check in this SQL Fiddle sample.

Using a query in Microsoft Access to compare two fields and find multiple matching values

I have a problem I am trying to solve using a query instead of VBA.
I have two fields which we'll call "FPC" and "Code". Both fields contain numbers. An FPC value will match a Code value. What I want to make sure is that once an FPC value matches a Code value, the same FPC value does not match up with a DIFFERENT Code Value and vice versa - Once a Code Value is used, I don't want the Code Value to match up with more than one FPC.
It is important to note that there are duplicate values used in both fields.
Here is an example:
FPC CODE
1 12
1 12
1 14
2 16
3 11
3 11
4 17
5 19
6 16
There are two errors here:
1. The FPC "1" is matched up with two different Code Values.
2. The Code "16" is matched up with two different FPC values.
Please let me know your suggestions. I was thinking a query would help, and then running VBA to pull the results (there is tens of thousands of records).
What is the purpose of the query? Just to identify problems? If yes then something like
select FPC, count(distinct(CODE)) from tableName where count(distinct(CODE)) >1 group by FPC
(and the converse query for CODE vs. FPC) should be OK.

Combine rows of data into one cell, separated by quotes and comma

I have the following data:
2
3
4
5
6
7
8
9
10
apple
green
hi
I would like to consolidate all of this into one value, like so:
'2','4','5','6','7','8','9','10','apple','green','hi'
So I need to take data from rows, paste them into an Excel file, run this macro and get them all in one cell (with commas and quotes). I will then put them in a WHERE IN clause in an SQL statement for faster searches. If there is an easier way or a tool, please let me know.
Here is the intended use in SQL:
Select *
from Blah..Table
where IDs in
(
'2','4','5','6','7','8','9','10','apple','green','hi'
)
I wrote a user defined function called JoinRange that does that. You can get the code here
http://www.dailydoseofexcel.com/archives/2012/03/31/joinrange-update/
You would use it like
=joinrange(A1:A12,,"','","'","'")
which gives the result
'2','3','4','5','6','7','8','9','10','apple','green','hi'
You can also call it from VBA.