字符串数值搜索问题

wokaoniya 2011-07-08 05:53:21
有一张表,里面有个字段是Nvarchar类型,储存的数值例如:第一列:12/31/75 第二列:34/67/94/113。搜索的时侯输入一个数值15。那么搜索的范围就是5-25,第一列的12符合这个范围,第一列就搜出来,例如输入70.第一列的75,和第二列的67都符合搜索范围。都搜出来,这要怎么做。各位高手帮帮忙。
...全文
137 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ayanamikula 2011-07-11
  • 打赏
  • 举报
回复
mark
weike021996 2011-07-10
  • 打赏
  • 举报
回复
真猛,高手
叶子 2011-07-09
  • 打赏
  • 举报
回复
如果不想使用临时表,或是表变量循环生成匹配数据的话
可以这样:

set nocount on
declare @table table (colname varchar(12))
insert into @table
select '12/31/75' union all
select '34/67/94/113' union all
select '1/56/3/16' union all
select '34/23/12/24' union all
select '90/34/45/47'

declare @i int;set @i=70--设置参数

select a.* from @table a left join master..spt_values b
on charindex('/'+ltrim(b.number)+'/','/'+a.colname+'/')>0
where b.[type]='P' and number between @i-10 and @i+10
and b.number is not null

/*
colname
------------
12/31/75
34/67/94/113
*/
  • 打赏
  • 举报
回复
学习了
叶子 2011-07-09
  • 打赏
  • 举报
回复

set nocount on
declare @table table (colname varchar(12))
insert into @table
select '12/31/75' union all
select '34/67/94/113' union all
select '1/56/3/16' union all
select '34/23/12/24' union all
select '90/34/45/47'

declare @i int;set @i=70
declare @j int;set @j=@i-10
declare @t table(id int)
while @j<=@i+10
begin
insert into @t select @j
set @j=@j+1
end
select * from @t
select a.* from @table a left join @t b
on charindex('/'+ltrim(b.id)+'/','/'+a.colname+'/')>0
where b.id is not null
/*
colname
------------
12/31/75
34/67/94/113
*/

上面少了个等号,补上。。。
叶子 2011-07-09
  • 打赏
  • 举报
回复

set nocount on
declare @table table (colname varchar(12))
insert into @table
select '12/31/75' union all
select '34/67/94/113' union all
select '1/56/3/16' union all
select '34/23/12/24' union all
select '90/34/45/47'

declare @i int;set @i=70
declare @j int;set @j=@i-10
declare @t table(id int)
while @j<@i+10
begin
insert into @t select @j
set @j=@j+1
end

select a.* from @table a left join @t b
on charindex('/'+ltrim(b.id)+'/','/'+a.colname+'/')>0
where b.id is not null
/*
colname
------------
12/31/75
34/67/94/113
*/
wokaoniya 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 laowang134 的回复:]
1.是第一行还是第一列?
2.范围是加减10?
[/Quote]

不要意思,是行,打错了打错了
xyytuo 2011-07-09
  • 打赏
  • 举报
回复
laowang134 2011-07-08
  • 打赏
  • 举报
回复
create table #tb(val varchar(200))
insert into #tb
select '12/25/35' union all
select '40/60/80'

go

create table #tb2(val int)
insert into #tb2
select 11 union all
select 12 union all
select 13 union all
select 14 union all
select 15 union all
select 16 union all
select 17 union all
select 18 union all
select 19 union all
select 20 union all
select 21 union all
select 22 union all
select 23 union all
select 24 union all
select 25 union all
select 26 union all
select 27 union all
select 28 union all
select 29 union all
select 30
go


select * from #tb a where exists(select 1 from #tb2 where a.val like '%'+cast(val as varchar)+'%')

/*
12/25/35
*/
--借助临时表,根据输入的数生成临时表
laowang134 2011-07-08
  • 打赏
  • 举报
回复
1.是第一行还是第一列?
2.范围是加减10?

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧