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)
 Select query for digit.

Author  Topic 

lesteryh
Starting Member

2 Posts

Posted - 2006-04-05 : 23:17:09
Hi all,
Here the something that i wanted to do, for example I have a 'tableA' and 1 of the field in this table call 'fieldA' and the value stored in this field is always 4 number ex:(1234) , so now my question is how can I use 'SELECT' query to return the records where the fieldA contain all the digit that I want? for ex: '1234' the record will match if the field contain '2134' or '4321' or '3421' and so on.....

So for my bad explaination, hopefully some1 with able to help me.

Thanks.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-06 : 00:05:20
what is the datatype for fieldA ?



KH


Go to Top of Page

lesteryh
Starting Member

2 Posts

Posted - 2006-04-06 : 00:11:56
Data type is string.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-06 : 01:01:25
Maybe there is a better way. But the brain is not working well when low on fuel.

create function dbo.search
(
@search char(4)
)
returns table
as
return
(
select substring(@search, n1, 1) + substring(@search, n2, 1) + substring(@search, n3, 1) + substring(@search, n4, 1) as search
from
(
select a.n as n1, b.n as n2, c.n as n3, d.n as n4
from
(
select 1 as n union all select 2 as n union all select 3 as n union all select 4
) a
inner join
(
select 1 as n union all select 2 as n union all select 3 as n union all select 4
) b
on a.n <> b.n
inner join
(
select 1 as n union all select 2 as n union all select 3 as n union all select 4
) c
on a.n <> c.n
and b.n <> c.n
inner join
(
select 1 as n union all select 2 as n union all select 3 as n union all select 4
) d
on a.n <> d.n
and b.n <> d.n
and c.n <> d.n
) num
)
-- select * from dbo.search('1234')




declare @tableA table
(
fieldA char(4)
)
declare @search char(4)
select @search = '1234'

insert into @tableA
select '1234' union all
select '2134' union all
select '4321' union all
select '3421' union all
select '2345'

select *
from @tableA a
where fieldA in (select search from dbo.search(@search))




KH


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-06 : 04:15:58
Why do you have this requirement?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -