简体   繁体   中英

Exclude records with specific value '*' SQL SELECT

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:

  1. 71A022
  2. 051116
  3. 070505PRO
  4. *031620
  5. 458508
  6. 501315
  7. *070247PE
  8. 370002
  9. 070278STU
  10. *CO20302

I suspect I may not be able to use the * as an excluding factor

Any thoughts would be appreceated

try this

 SELECT prdcod
    FROM prdtbl
    WHERE prodcod not like '*%';

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

SELECT prdcod
FROM prdtbl
WHERE prodcod NOT LIKE '*%';

instead of using "<>" operator, use "not like":

Select prdcod
From prdtbl
Where prodcod not like '*%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM