提取字段部分内容 sql如何写 ? 字段内容如下:

chinawcs 2008-03-17 09:29:12
field1

照片21cm
彩图12cm
有光盘
21cm
null
....


想把这个字段中的 汉字 和其它的分开为两个字段(部分) 如:照片21cm 分离开为 照片 和 21cm

能实现吗 ?

...全文
211 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JL99000 2008-03-17
  • 打赏
  • 举报
回复
这样的sql 我还是第一次用 最好能有点解释 能知道为什么 就最好了

'%[^吖-咗]%' 这个表达式 是什么意思呢 ?

谢谢 。 这次给你分了

跟着您 学到不少东西

---我告诉你吧
他的作用是做一个通陪符,意思就是要查找的字符串应该是以中文开头
dawugui 2008-03-17
  • 打赏
  • 举报
回复
--如果中文全部在左边且连续,直接用一楼即可.

如果是中英文穿叉,用下面的函数.

create table tb(col char(20))
insert into tb values('照片21cm')
insert into tb values('彩图12cm')
insert into tb values('有光盘')
insert into tb values('21cm')
insert into tb values(null)
go

create function f_get_c(@oldstr varchar(100)) returns varchar(100)
as
begin
declare @i int
set @i = 1
while @i <= len(@oldstr)
if substring(@oldstr, @i, 1) like('[^吖-咗]')
set @oldstr = replace(@oldstr, substring(@oldstr, @i, 1), '')
else
set @i = @i +1
return @oldstr
end
go

create function f_get_e(@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 dbo.f_get_c(col) col_c , dbo.f_get_e(col) col_e from tb where col like('%[^吖-咗]%') or col like('%[^a-z,A-Z,0-9]%')
union all
select col1 = null,col2 = null from tb where col is null

drop table tb
drop function f_get_e
drop function f_get_c

/*
col_c col_e
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
照片 21cm
彩图 12cm
有光盘
21cm
NULL NULL

(所影响的行数为 5 行)
*/
dawugui 2008-03-17
  • 打赏
  • 举报
回复
create table tb(col char(20))
insert into tb values('照片21cm')
insert into tb values('彩图12cm')
insert into tb values('有光盘')
insert into tb values('21cm')
insert into tb values(null)
go

create function f_get_c(@oldstr varchar(100)) returns varchar(100)
as
begin
declare @i int
set @i = 1
while @i <= len(@oldstr)
if substring(@oldstr, @i, 1) like('[^吖-咗]')
set @oldstr = replace(@oldstr, substring(@oldstr, @i, 1), '')
else
set @i = @i +1
return @oldstr
end
go

create function f_get_e(@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 id = identity(int , 1 , 1) , dbo.f_get_c(col) col_c into tmp1 from tb where col like('%[^吖-咗]%')
select id = identity(int , 1 , 1) , dbo.f_get_e(col) col_e into tmp2 from tb where col like('%[^a-z,A-Z,0-9]%')

select m.col_c , n.col_e from tmp1 m , tmp2 n where m.id = n.id
union all
select col1 = null,col2 = null from tb where col is null

drop table tb , tmp1 , tmp2
drop function f_get_e
drop function f_get_c

/*
col_c col_e
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
照片 21cm
彩图 12cm
有光盘
21cm
NULL NULL

(所影响的行数为 5 行)
*/
chinawcs 2008-03-17
  • 打赏
  • 举报
回复
这样的sql 我还是第一次用 最好能有点解释 能知道为什么 就最好了

'%[^吖-咗]%' 这个表达式 是什么意思呢 ?

谢谢 。 这次给你分了

跟着您 学到不少东西
山之魂2 2008-03-17
  • 打赏
  • 举报
回复
Mark
dawugui 2008-03-17
  • 打赏
  • 举报
回复
create table tb(col char(20))
insert into tb values('照片21cm')
insert into tb values('彩图12cm')
insert into tb values('有光盘')
insert into tb values('21cm')
insert into tb values(null)
go

select col1=left(col,patindex('%[^吖-咗]%',col)-1),
col2=substring(col,patindex('%[^吖-咗]%',col),len(col))
from tb
where patindex('%[^吖-咗]%',col) > 0
union all
select col1 = null,col2 = null from tb where col is null

drop table tb

/*
col1 col2
-------------------- --------------------
照片 21cm
彩图 12cm
有光盘
21cm
NULL NULL

(所影响的行数为 5 行)
*/

34,838

社区成员

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

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