Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 filter the string that has integer> 1

Author  Topic 

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-09-22 : 16:51:56
Hi

I have a field called customer history where the data is stored in this way
----11000000 or -00022122122.

i don't want to show the record that contain 3 1's in it(ex ----11001000) or any integer that is greater than 1(ex:-00022122122).
How do i filter those records.

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-23 : 00:14:47
WHERE LEN(Col1) - LEN(REPLACE(Col1, '1', '')) <> 3



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-09-23 : 15:28:05
thanks for the respone but it did not help.
Is there something else i need to add to that.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-09-23 : 18:15:34
Does this work or do you have some other requirements or data that you need to handle?
DECLARE @Yak TABLE (Col1 VARCHAR(20))

INSERT @Yak
SELECT '----11000000'
UNION ALL SELECT '-00022122122'
UNION ALL SELECT '-00020122122'
UNION ALL SELECT '-00021122122'

SELECT
*
FROM
@Yak
WHERE
LEN(Col1) - LEN(REPLACE(Col1, '1', '')) <> 3
AND CAST(REPLACE(Col1, '-', '') AS INT) < 22122122
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-09-24 : 09:50:21
it can also have digit 3 in the customer history field. The example is just an random one i picked.

thanks for the response though!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-24 : 10:51:09
How about you tell us the full story?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-24 : 11:43:22
I don't need the full story to throw darts in the dark How about this?
filters out any value with 3 or more occurances of any single numeric digit (besides 0)

declare @s varchar(155)
set @s = '-00022122122'

declare @t table (s varchar(200))
insert @t (s)
SELECT '----11000000'
UNION ALL SELECT '-00022122122'
UNION ALL SELECT '-00020122122'
UNION ALL SELECT '-00021122122'
UNION ALL SELECT '-0001234567899999'
UNION ALL SELECT '-0001234567899'

select t.s
from @t t
cross apply (
select case
when exists (
select d.c
,count(*)
from (
select n.number
,substring(t.s, n.number, 1) c
from master..spt_values n
where n.type = 'p'
and n.number > 0
and n.number <= len(t.s)
) d
where d.c like '%[1-9]%'

group by c
having count(*) > 3
) then 1
else 0
end HasMoreThan3
) ca
where ca.HasMoreThan3 = 0

output:
s
-----------------------------
----11000000
-0001234567899


Be One with the Optimizer
TG
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-09-24 : 16:14:05
i am sorry .May be i was not clear.

What I need is to filter the string that
1)if the string has more 3 or more than 3 occurences of interger 1 in it
ex: 10010010 or 1111000 or 11001100 etc
2)if the string contains atleast one integer greater than 1
ex 000021000, 0330000,0112000 etc

i want to filter out those strings . hope i am clear this time.

i only want records that have 0's and 1's in the string but not more than three 1's
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-09-24 : 16:15:00
quote:
Originally posted by akpaga

i am sorry .May be i was not clear.

What I need is to filter the string that
1)if the string has 3 occurrences or more than 3 occurrences of interger 1 in it
ex: 10010010 or 1111000 or 11001100 etc
2)if the string contains atleast one integer greater than 1
ex 000021000, 0330000,0112000 etc

i want to filter out those strings . hope i am clear this time.

i only want records that have 0's and 1's in the string but not more than three 1's


Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-24 : 16:31:23
select ..
from ...
where not (col like '%1%1%1%' or col like '%[2-9]%')



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-24 : 16:38:20
WHERE Col1 NOT LIKE '%[^01]%' AND Col1 NOT LIKE '%1%1%1%1%'



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-24 : 16:51:37
WHERE Col1 NOT LIKE '%[^01]%' AND Col1 NOT LIKE '%' + REPLICATE('1%', 4)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -