小问题,有条件的读取数据

北京不不 2006-02-28 12:34:12
一个数据库里有个字段txt
txt
1.2.33333
1.2.14
1.2.344
1.2.5
1.2.3.4
1.2.3.5
1.2.3.4.1
1.2.333.4
1.2.3333.444444
我要取类似于1.2.*当于我只取三个数字的类似1.2.的
如。。
1.2.33333
1.2.14
1.2.344
1.2.5
我不想取
1.2.3.4
1.2.3.5
1.2.3.4.1
1.2.333.4
1.2.3333.444444
SQL语句该怎么写。
...全文
123 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lw1a2 2006-02-28
  • 打赏
  • 举报
回复
create table #t(txt varchar(30))
go

insert into #t values('1.2.33333')
insert into #t values('1.2.14')
insert into #t values('1.2.344')
insert into #t values('1.2.5')
insert into #t values('1.2.3.4')
insert into #t values('1.2.3.5')
insert into #t values('1.2.3.4.1')
insert into #t values('1.2.333.4')
insert into #t values('1.2.3333.444444')
go

select *
from #t
where txt like '1.2.%' and txt not like '1.2.%.%'
子陌红尘 2006-02-28
  • 打赏
  • 举报
回复
select * from 表 where txt like '1.2.%' and len(txt)=len(replace(txt,'.',''))+2
子陌红尘 2006-02-28
  • 打赏
  • 举报
回复
ACCESS里的实现方法:
select * from 表 where txt like '1.2.*' and right(txt,len(txt)-4) not like '*.*'

34,588

社区成员

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

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