得到字符串列表指定位置的字符

各位大侠,数据库是别人设计的,现在的情况如下,请求解决问题的办法

这是我现在的情况:

declare @UserItem table(UserItemID int,CateName varchar(50),OtherDemo text)

insert @UserItem(UserItemID,CateName,OtherDemo)
values(1,'张三','50&黄石市小河镇&0762-2262626')
insert @UserItem(UserItemID,CateName,OtherDemo)
values(2,'李四','35&广州市&020-2262626')
insert @UserItem(UserItemID,CateName,OtherDemo)
values(3,'博士','25&青岛&0456-2262626')
insert @UserItem(UserItemID,CateName,OtherDemo)
values(4,'学士','25&北京&010-2262626')

select * from @UserItem

我想得到如下的结果:
UserItemID CateName 年龄 地址 联系电话
1 张三 50 黄石市小河镇 0762-2262626
2 李四 35 广州市 020-2262626
3 博士 25 青岛 0456-2262626
4 学士 25 北京 010-2262626
---------------------------------------------------------------

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_GetStr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_GetStr]
GO

/*--得到字符串列表指定位置的字符

可以自定义字符串列表的分隔符
如果取数位置超出的范围,返回空字符串

--邹建 2005.04(引用请保留此信息)--*/

/*--调用示例
--测试数据
declare @UserItem table(UserItemID int,CateName varchar(50),OtherDemo text)
insert @UserItem
select 1,'张三','50&黄石市小河镇&0762-2262626' union all
select 2,'李四','35&广州市&020-2262626' union all
select 3,'博士','25&青岛&0456-2262626' union all
select 4,'学士','25&北京&010-2262626'

--分拆
select UserItemID,CateName
,年龄=dbo.f_GetStr(OtherDemo,1)
,地址=dbo.f_GetStr(OtherDemo,2)
,联系电话=dbo.f_GetStr(OtherDemo,3)
from @UserItem

/*--结果

UserItemID CateName 年龄 地址 联系电话
----------- ------------ ---------- ------------- ---------------
1 张三 50 黄石市小河镇 0762-2262626
2 李四 35 广州市 020-2262626
3 博士 25 青岛 0456-2262626
4 学士 25 北京 010-2262626

(所影响的行数为 4 行)
--/
--
/

---------------------------------------------------------------

注意中文在数据中处理的问题,所以处理的时候,一律改用unicode双字节处理,避免半个汉字和单双字节在处理中转换的麻烦.

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus