I am in the processing of preparing for migration to a new database system for a stock based retail store. In the current database products which have been deactivated have a leading * added to the record.
The owners do not want to bring deactivated products into the new system so this leading * is my only reference point to work from.
I need to create a SELECT query that will exclude products that have a leading * but so for to no avail.
I have tried the below
SELECT prdcod
FROM prdtbl
WHERE prodcod<>'*%';
The first 10 results returned are:
71A022
051116
070505PRO
*031620
458508
501315
*070247PE
370002
070278STU
*CO20302
I suspect I may not be able to use the * as an excluding factor
Any thoughts would be appreceated
This can be done in so many ways..
Using NOT LIKE
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%'
Using LEFT/SUBSTRING
SELECT prdcod
FROM prdtbl
WHERE LEFT(prodcod,1) <> '*' -- SUBSTRING(prodcod from 1 for 1) <> '*'
Note : Not Like is preferred approach if you have a Index on prodcod column
try this
SELECT prdcod
FROM prdtbl
WHERE prodcod not like '*%';
SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%';
instead of using "<>" operator, use "not like":
Select prdcod
From prdtbl
Where prodcod not like '*%';
Related
I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other
I have some records in a column that contain IDs and some of these records contain multiple IDs separated by commas. Additionally there are some records where I have ",3" and ",2" when they should simply be "3" and "2". I do not have write privileges in this DB so updating those records is not an option.
I am trying to write a query that returns records that have a comma where the value to the left of any comma in the record is greater than 0 e.g. "2,3", "2,3,12" etc but NOT ",3" or ",2".
What would this expression look like in MS Access?
Thanks in advance.
If you want to remove the starting comma from the records when you return them, you can do so using a simple query:
SELECT IIF(MyField LIKE ",*", Right(MyField, Len(MyField)-1), MyField)
FROM MyTable
To answer your original question, you could simply use Val:
SELECT * FROM YourTable WHERE Val([YourField]) > 0
I would simply use:
select t.*
from t
where val not like ",*";
This doesn't handle the 0 part, but you don't give any examples in your answer. Perhaps this answers that part:
select t.*
from t
where val not like ",*" and val not like "*0,*";
I have ,for example, this table in a Microsoft Access database:
id numeric
context text
numberfield numeric
I want to select every record that ends with 9 in the column"numberfield". This gives a problem because it is a numeric field and as a result I can not use the following SQL:
select * from table where numberfield like "%9"
A solution is that I change the numberfield to a text. But this gives a problem because there are several users and the change might give a problem in the future. Is there an option to select on the ending when it is a number field?
That sound a little fishy.. are you sure you can use that query? Don't know about Access but almost any other DBMS allows it.
If it really doesn't work, you can do this:
select * from table where STR(numberfield) like "*9"
EDIT: Maybe it didn't work because you used % which is used with * in Access :
select * from table where numberfield like "*9"
Numbers are numbers, so use Mod for this:
select * from table where numberfield mod 10 = 9
Instead of casting to string and comparing, just extract the rightmost digit with a MOD operation.
Edit your query as follows:
SELECT *
FROM table
WHERE ((([numberfield] Mod 10)=9));
I'm currently making a call to an SQL database that counts all entries where the cell starts with NOI, but ends with anything else.
I thought using the below would work, but it doesn't seem to, anyone have any ideas? I know the % sign is the wildcard for foxpro, I don't know if this is the same in SQL
SELECT COUNT * FROM DIARY WHERE PTNOTE = 'NOI%'
You have to use LIKE if you want to use the wildcard characters:
SELECT COUNT(*) FROM DIARY WHERE PTNOTE LIKE 'NOI%'
(also added the parantheses around *)
You are missing parentheses:
SELECT COUNT(*)
FROM DIARY
WHERE PTNOTE = 'NOI%';
It is not the case even in Foxpro. You should use parentheses and "like":
SELECT COUNT(*) FROM DIARY WHERE PTNOTE like 'NOI%'
Anyone got any idea why this doesn't work. Im at a loss
The following
SELECT * FROM tblCustomerDetails WHERE AccountNo='STO00900'
Returns nothing however if i run the same query with any othe accoutn number it works.
and this account will show when i run
SELECT TOP 10 * FROM tblCustomerDetails ORDER BY ID desc
Picture explains it better.
Thanks
Try as Notulysses suggested, but I would recommend it a bit differently:
SELECT * FROM tblCustomerDetails WHERE LTRIM(RTRIM(AccountNo)) = 'STO00900'
The LIKE operator will likely match more rows than you need (if te AccountNo column is not unique), so I'd go with trimming the whitespaces and then checking for a specific account.
There may be some space in the entry either in the start or at the end ,try to trim both ends of the entry.
Try
SELECT * FROM tblCustomerDetails WHERE AccountNo LIKE '%STO00900%'
As there can be hidden characters.