假如表A:
[物料名称]
10S/2小化纤
10S涤麻
10S中化纤
100D网络丝
特种纱我要把[物料名称]字段分成两个字段,后面汉字为一个字段,汉字前面为另一个字段。如得到表B:
字段1 字段2
10S/2 小化纤
10S 涤麻
10S 中化纤
特种纱
[物料名称]
10S/2小化纤
10S涤麻
10S中化纤
100D网络丝
特种纱我要把[物料名称]字段分成两个字段,后面汉字为一个字段,汉字前面为另一个字段。如得到表B:
字段1 字段2
10S/2 小化纤
10S 涤麻
10S 中化纤
特种纱
if object_id('[tb]') is not null drop table [tb]
create table [tb]([物料名称] varchar(11))
insert [tb]
select '10S/2小化纤' union all
select '10S涤麻' union all
select '10S中化纤' union all
select '100D网络丝' union all
select '特种纱'select substring(物料名称,1,patindex('%[吖-做]%',物料名称)-1) ,
substring(物料名称,patindex('%[吖-做]%',物料名称),len(物料名称))
from [tb]
--------------------------
10S/2 小化纤
10S 涤麻
10S 中化纤
100D 网络丝
特种纱
-- Author :SQL77(只为思齐老)
-- Date :2010-01-13 21:29:15
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
go
create table [TB]([物料名称] varchar(11))
insert [TB]
select '10S/2小化纤' union all
select '10S涤麻' union all
select '10S中化纤' union all
select '100D网络丝' union all
select '特种纱'
--------------开始查询--------------------------select
SUBSTRING(物料名称,1,PATINDEX('%[吖-做]%',物料名称)-1)AS A,
STUFF(物料名称,1,PATINDEX('%[吖-做]%',物料名称)-1,'')AS B
from [TB]
WHERE PATINDEX('%[吖-做]%',物料名称)>0
----------------结果----------------------------
/* (所影响的行数为 5 行)A B
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10S/2 小化纤
10S 涤麻
10S 中化纤
100D 网络丝
特种纱(所影响的行数为 5 行)
*/
left(物料名称,patindex('%[吖-做]%',物料名称)-1) ,
substring(物料名称,patindex('%[吖-做]%',物料名称),len(物料名称))
from
[tb]
select
left(物料名称,patindex('%[吖-做]%',物料名称)-1),
right(物料名称,len(物料名称) - patindex('%[吖-做]%',物料名称)+1)
from tb%[吖-做]%
可以匹配绝大部分常用汉字,按照你的要求,应该没有问题
if object_id('[A]') is not null drop table [A]
create table [A] ([物料名称] varchar(11))
insert into [A]
select '10S/2小化纤' union all
select '10S涤麻' union all
select '10S中化纤' union all
select '100D网络丝' union all
select '特种纱'select 字段1=left(物料名称,patindex('%[吖-咗]%',物料名称)-1),
字段2=substring(物料名称,patindex('%[吖-咗]%',物料名称),len(物料名称)) from [A]--结果:
字段1 字段2
----------- -----------
10S/2 小化纤
10S 涤麻
10S 中化纤
100D 网络丝
特种纱
if object_id('[tb]') is not null drop table [tb]
create table [tb]([物料名称] varchar(11))
insert [tb]
select '10S/2小化纤' union all
select '10S涤麻' union all
select '10S中化纤' union all
select '100D网络丝' union all
select '特种纱'select substring(物料名称,1,patindex('%[吖-做]%',物料名称)-1) ,
substring(物料名称,patindex('%[吖-做]%',物料名称),len(物料名称))
from [tb]
--------------------------
10S/2 小化纤
10S 涤麻
10S 中化纤
100D 网络丝
特种纱