Avatar of Scott S
Scott S
Flag for United States of America asked on

Like statement pulling too much data

(((cv.STR_VALUE)) Like ('%1,%')
keeps pulling 10, 11, 12 etc is there a way to limit this to just 1,
SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
Scott Fell

8/22/2022 - Mon
Ryan Chong

keeps pulling 10, 11, 12 etc is there a way to limit this to just 1, 

if you mean just to return 1 record for such similarity, then just add the TOP clause into your select query, such as:

Select Top 1 from yourTable cv where cv.STR_VALUE Like '%1,%'

Open in new window

Scott S

ASKER
No let me restate the field is multiple entries 1,2,3--10,11,12  I need to pull just 1, the statement I have pulls 1, 10, 11,
Ryan Chong

sorry that's not so clear to me.

can you provide the input and output value? is it to pulling data or format the values at field level?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Scott Fell




CREATE TABLE TableOne
    ([productIDs] varchar(13))
;


INSERT INTO TableOne
    ([productIDs])
VALUES
    ('1,10,3'),
    ('4,11,2'),
    ('5,8,1')
;

Open in new window

 
SELECT
  productIDs


FROM
  TableOne
 
  WHERE
   
  '1' IN (SELECT value FROM STRING_SPLIT(productIDs, ','));  

Open in new window



|------------|
|     1,10,3 |
|      5,8,1 |
       

Open in new window

https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver15

Sharath S

try this.

','+cv.str_value+',' like '%,1,%'

Open in new window

here is an example:
create table t(STR_VALUE varchar(20))
insert into t values ('1,2,3'),('10,11,12'),('4,3,1')
select * from t where ','+str_value+',' like '%,1,%'
/*
1,2,3
4,3,1
*/

Open in new window

PortletPaul

I believe the question is asking how to ignore numbers other than 1

I.e. how to ignore 10 or to ignore 111 or ignore 12 etc.

It would be great if the question could be clarified and some sample data provided.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Andrei Fomitchev

SELECT * FROM table WHERE LEFT(column,1) = '1'

or

DECLARE @value NVarChar(200) = '1,2,3--10,11,12'
SELECT * FROM string_split(@value,',') WHERE value = '1'

IF Exists(SELECT * FROM string_split(@value,',') WHERE value = '1') PRINT '1 exists'
ELSE PRINT '1 does not exists'
Scott S

ASKER
(((cv.STR_VALUE)) Like ('%1,%')
(((cv.STR_VALUE)) Like ('%1,')
 

 
ASKER CERTIFIED SOLUTION
Scott S

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott Fell

Does that work if the data stored looks like ('5,8,1')

In other words, the '1' is the last item and does not have a comma at the end?
Your help has saved me hundreds of hours of internet surfing.
fblack61