sql server - SQL: How to find product codes -


we have database (mssql) contains products table, products put in way in product id first 4 letters customer specific code. example:

  • 123070004400b
  • 002070004400b
  • 012316003030e

etc.

the first 4 letters customer code, while later part product code can overlap. same customer can order products same code, keep our stock separate this. in database @ least.

is there way make query tell example product 07004400b exists in more 1 customers entry?

use substring() extract produce code, , group-by having find hits:

select substring(product_id, 5, len(product_id)) code products group substring(product_id, 5, len(product_id)) having count(*) > 1 

if want specific one, add clause:

select substring(product_id, 5, len(product_id)) code products substring(product_id, 5, len(product_id)) = '0700400b' group substring(product_id, 5, len(product_id)) having count(*) > 1 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -