求一个sql查询的问题

liujun3512159 2011-05-25 10:20:48
如:
select * from createTable where xh>='445-2' and xh<'445-20'

这是sqlserver2000下测试的,其中字段xh是varchar的,实际上数据库里xh字段的内容是这样的:
445-1
445-2
445-3
445-3
。。。。。。。

445-20

但是,我通过这个语句查询的时候,只查到445-2这一条记录,我实际上是想查询这个范围里的所有数据,请求帮助
...全文
85 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcw321321 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用楼主 liujun3512159 的回复:]
如:
select * from createTable where xh>='445-2' and xh<'445-20'

这是sqlserver2000下测试的,其中字段xh是varchar的,实际上数据库里xh字段的内容是这样的:
445-1
445-2
445-3
445-3
。。。。。。。

445-20

但是,我通过这个语句查询的时候,只查到445-2这一条……
[/Quote]
你把 ‘-’去掉,然后再比
liang145 2011-05-25
  • 打赏
  • 举报
回复

declare @s as nvarchar(20)
declare @e as nvarchar(20)
set @s='111-3'
set @e='445-3000'
select * from #tb where cast(left(col,charindex('-',col)-1) as int) between
cast(left(@s,charindex('-',@s)-1) as int) and
cast(left(@e,charindex('-',@e)-1) as int)
and ((left(col,charindex('-',col)-1)<>left(@s,charindex('-',@s)-1))
or
(left(col,charindex('-',col)-1)=left(@s,charindex('-',@s)-1)
and cast(stuff(col,1,charindex('-',col),'') as int)>=cast(stuff(@s,1,charindex('-',@s),'') as int)))
and ((left(col,charindex('-',col)-1)<>left(@e,charindex('-',@e)-1))
or
(left(col,charindex('-',col)-1)=left(@e,charindex('-',@e)-1)
and cast(stuff(col,1,charindex('-',col),'') as int)<=cast(stuff(@e,1,charindex('-',@e),'') as int)))
order by left(col,charindex('-',col)-1),len(stuff(col,1,charindex('-',col),''))

12楼写错了~~
liang145 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 liujun3512159 的回复:]

这些方法貌似都不是很好的,如果序号是
111-1
222-3
445-1

。。。。。。

445-20

我现在输入查询条件 222-1 和445-20
你怎么办?
[/Quote]


create table #tb(col nvarchar(50))
insert #tb
select '111-1' union all
select '222-3' union all
select '111-88' union all
select '222-66' union all
select '445-1' union all
select '445-2' union all
select '445-3' union all
select '445-4' union all
select '445-5' union all
select '445-6' union all
select '445-3000' union all
select '445-20' union all
select '446-30' union all
select '446-2' union all
select '445-23'


declare @s as nvarchar(20)
declare @e as nvarchar(20)
set @s='111-3'
set @e='446-2'
select * from #tb where cast(left(col,charindex('-',col)-1) as int) between
cast(left(@s,charindex('-',@s)-1) as int) and
cast(left(@e,charindex('-',@e)-1) as int)
and ((left(col,charindex('-',col)-1)<>left(@s,charindex('-',@s)-1))
or
(left(col,charindex('-',col)-1)=left(@s,charindex('-',@s)-1) and cast(stuff(col,1,charindex('-',col),'') as int)>=cast(stuff(@s,1,charindex('-',@s),'') as int)))
and ((left(col,charindex('-',col)-1)<>left(@e,charindex('-',@s)-1))
or
(left(col,charindex('-',col)-1)=left(@e,charindex('-',@s)-1) and cast(stuff(col,1,charindex('-',col),'') as int)<=cast(stuff(@e,1,charindex('-',@e),'') as int)))
order by col
yibey 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 liujun3512159 的回复:]

这些方法貌似都不是很好的,如果序号是
111-1
222-3
445-1

。。。。。。

445-20

我现在输入查询条件 222-1 和445-20
你怎么办?
[/Quote]

哥们你又不吧要求说清楚,,谁知道你2边都要比较的啊
haysey 2011-05-25
  • 打赏
  • 举报
回复
select * from createTable
where left(xh,3)>222 and left(xh,3)<445
or left(xh,3)=222 and cast(right(xh,len(xh)-4) as int)>1
or left(xh,3)=445 and cast(right(xh,len(xh)-4) as int)<20
liang145 2011-05-25
  • 打赏
  • 举报
回复

create table #tb(col nvarchar(50))
insert #tb
select '445-1' union all
select '445-2' union all
select '445-3' union all
select '445-4' union all
select '445-5' union all
select '445-6' union all
select '445-7' union all
select '445-20' union all
select '500-1' union all
select '500-2' union all
select '445-23'

select * from #tb where left(col,3)='445'
and cast(stuff(col,1,4,'') as int) between 1 and 20
liujun3512159 2011-05-25
  • 打赏
  • 举报
回复
这些方法貌似都不是很好的,如果序号是
111-1
222-3
445-1

。。。。。。

445-20

我现在输入查询条件 222-1 和445-20
你怎么办?
hlf1989 2011-05-25
  • 打赏
  • 举报
回复
select * from createTable where xh like ('445-[2-9]') or xh like ('445-1[0-9]')
hlf1989 2011-05-25
  • 打赏
  • 举报
回复
select * from createTable where xh like ('445-[2-9]' or  '445-1[0-9]')
快溜 2011-05-25
  • 打赏
  • 举报
回复
with cte as 
(
select row_number() over(order by getdate()) no,* from createTable
)

select * from cte where no>=(select min(no) from cte where xh='445-2')
and no<=(select max(no) from cte where xh='445-20')
yibey 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yibey 的回复:]

SQL code

where
left(xh,4)='445-' and (cast(right(xh,len(xh)-4) as int) between 1 and 20)

---xh>='445-2' and xh<'445-20'
[/Quote]
如果是2的话自己把上面的范围改下
yibey 2011-05-25
  • 打赏
  • 举报
回复

where
left(xh,4)='445-' and (cast(right(xh,len(xh)-4) as int) between 1 and 20)

---xh>='445-2' and xh<'445-20'


WU_YEAH 2011-05-25
  • 打赏
  • 举报
回复
楼上手好快啊
jyh070207 2011-05-25
  • 打赏
  • 举报
回复
select * from createTable where xh like '445-2%'
gogodiy 2011-05-25
  • 打赏
  • 举报
回复
有几个就用几个LIKE,中间用OR连接就是了。
lxuan_025 2011-05-25
  • 打赏
  • 举报
回复
你把 ‘-’去掉,然后再比

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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