SQL 2000上如何实现正则查询某字段只包含英文或数字或某符号

vjlin 2011-09-29 02:47:51
如何在SQL 2000上实现 正则查询字段 Name 中只包含英文字母或数字或字母和数字组合或“.-”

例如:

Aabc1
5Dsc4
C6-hk
sd+9t
hsd(5]
号D.9
5T中e

结果:

Aabc1
5Dsc4
C6-hk
...全文
615 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
姐姐 2011-10-11
  • 打赏
  • 举报
回复
++++++感谢dawugui~~~~ 学习拉~~
thewitcher~ 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dawugui 的回复:]
SQL code
create table tb(col varchar(20))
insert into tb values('Aabc1')
insert into tb values('5Dsc4')
insert into tb values('C6-hk')
insert into tb values('sd+9t')
insert into tb values('hsd(5……
[/Quote]
+ 学习了
rmini 2011-09-29
  • 打赏
  • 举报
回复
收藏收藏
dawugui 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 vjlin 的回复:]
忘了说明字符个数是不固定的[/Quote]我6楼的函数可以不考虑位数.
-晴天 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 vjlin 的回复:]
引用 4 楼 qianjin036a 的回复:
SQL code
create table tb(col char(6))
insert into tb select 'Aabc1'
insert into tb select '5Dsc4'
insert into tb select 'C6-hk'
insert into tb select 'sd+9t'
insert into……
[/Quote]
那要用如上面6楼的函数.
vjlin 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qianjin036a 的回复:]
SQL code
create table tb(col char(6))
insert into tb select 'Aabc1'
insert into tb select '5Dsc4'
insert into tb select 'C6-hk'
insert into tb select 'sd+9t'
insert into tb select 'hsd(5]'
inse……
[/Quote]

忘了说明字符个数是不固定的
dawugui 2011-09-29
  • 打赏
  • 举报
回复
create table tb(col varchar(20))
insert into tb values('Aabc1')
insert into tb values('5Dsc4')
insert into tb values('C6-hk')
insert into tb values('sd+9t')
insert into tb values('hsd(5]')
insert into tb values('号D.9')
insert into tb values('5T中e')
go

--建立如下函数(非a-z A-Z 0-9 .,- , 之外的字符删除)
go
create function getnewstr(@oldstr varchar(100)) returns varchar(100)
as
begin
declare @i int
set @i = 1
while @i <= len(@oldstr)
if substring(@oldstr, @i, 1) like('[^a-z,A-Z,0-9,.,-]')
set @oldstr = replace(@oldstr, substring(@oldstr, @i, 1), '')
else
set @i = @i +1
return @oldstr
end
go


select m.* from tb m where not exists(select 1 from
(select col from tb where col like('%[^a-z,A-Z,0-9,.,-]%')) n where m.col = n.col)

drop table tb
drop function dbo.getnewstr

/*
col
--------------------
Aabc1
5Dsc4
C6-hk

(所影响的行数为 3 行)
*/
-晴天 2011-09-29
  • 打赏
  • 举报
回复
哇..........
今天终于装上了 2000,可以用2000了.
-晴天 2011-09-29
  • 打赏
  • 举报
回复
create table tb(col char(6))
insert into tb select 'Aabc1'
insert into tb select '5Dsc4'
insert into tb select 'C6-hk'
insert into tb select 'sd+9t'
insert into tb select 'hsd(5]'
insert into tb select '号D.9'
insert into tb select '5T中e'


go
select * from tb where col like '[a-z,0-9,-][a-z,0-9,-][a-z,0-9,-][a-z,0-9,-][a-z,0-9,-]'
/*
col
----------
Aabc1
5Dsc4
C6-hk

(所影响的行数为 3 行)

*/
go
drop table tb
-晴天 2011-09-29
  • 打赏
  • 举报
回复
按位 like.
--小F-- 2011-09-29
  • 打赏
  • 举报
回复
1楼的有点问题 楼主先忽略
--小F-- 2011-09-29
  • 打赏
  • 举报
回复
select * from [tb] where exists(select 1 from tb where PATINDEX('%[A-Z][a-z][0-9][.][_]%',col)>0)

22,199

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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