拆分字符串

B525068254 2015-11-03 10:17:26
select spmc from a

得到
170*1.63


我想分拆spmc 得到

select spmc,spmc1,spmc2 from a
170*1.63,170,1.63
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
西落兮 2015-11-03
  • 打赏
  • 举报
回复
引用 4 楼 B525068254 的回复:
[quote=引用 3 楼 pps873792861 的回复:]
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
这个不行吧 select spmc,spmc1,spmc2 from a 170*1.63,170,1.63 17*1.63,17,1.63 怎么办[/quote]
引用 4 楼 B525068254 的回复:
[quote=引用 3 楼 pps873792861 的回复:]
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
这个不行吧 select spmc,spmc1,spmc2 from a 170*1.63,170,1.63 17*1.63,17,1.63 怎么办[/quote] 或者用case语句
select spmc, case when CHARINDEX('*',spmc)>0 then SUBSTRING(spmc,1,CHARINDEX('*',spmc)-1)else spmc end spm1,
       case when CHARINDEX('*',spmc)>0 then SUBSTRING(spmc,CHARINDEX('*',spmc)+1,LEN(spmc)-CHARINDEX('*',spmc))else spmc end spm2
       from a
西落兮 2015-11-03
  • 打赏
  • 举报
回复
引用 4 楼 B525068254 的回复:
[quote=引用 3 楼 pps873792861 的回复:]
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
这个不行吧 select spmc,spmc1,spmc2 from a 170*1.63,170,1.63 17*1.63,17,1.63 怎么办[/quote]
引用 4 楼 B525068254 的回复:
[quote=引用 3 楼 pps873792861 的回复:]
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
这个不行吧 select spmc,spmc1,spmc2 from a 170*1.63,170,1.63 17*1.63,17,1.63 怎么办[/quote] 写错,应该是
select spmc,substring(spmc,1,3) spmc1,substring(spmc,5,8) spmc2  from a
或者你写个函数,到时候调用吧


create function split
(
 @str nvarchar(200),
 @i int 
)
returns nvarchar(200)
as
begin
 declare @len int
 declare @substr nvarchar(200)
 declare @wz int
 if CHARINDEX('*',@str)>0
 begin
 set @len= LEN(@str)
 set @wz=CHARINDEX('*',@str,1)
 if @i>0
 set @substr=SUBSTRING(@str,1,@wz-1)
 else
 set @substr=SUBSTRING(@str,@wz+1,@len-@wz)
 end
 else
 set @substr=@str
 
 return @substr
 
end
comma212164 2015-11-03
  • 打赏
  • 举报
回复
引用 6 楼 B525068254 的回复:
[quote=引用 5 楼 comma212164 的回复:]
select tname,left(tname,CHARINDEX('*',tname)-1),RIGHT(tname,LEN(tname)-CHARINDEX('*',tname))
 from test1 
(所影响的行数为 83 行) 服务器: 消息 536,级别 16,状态 3,行 1 向 substring 函数传递了无效的 length 参数。 有些可能没有*[/quote] 没有*的不拆分,就加个条件过滤一下
B525068254 2015-11-03
  • 打赏
  • 举报
回复
引用 5 楼 comma212164 的回复:
select tname,left(tname,CHARINDEX('*',tname)-1),RIGHT(tname,LEN(tname)-CHARINDEX('*',tname))
 from test1 
(所影响的行数为 83 行) 服务器: 消息 536,级别 16,状态 3,行 1 向 substring 函数传递了无效的 length 参数。 有些可能没有*
comma212164 2015-11-03
  • 打赏
  • 举报
回复
select tname,left(tname,CHARINDEX('*',tname)-1),RIGHT(tname,LEN(tname)-CHARINDEX('*',tname))
 from test1 
B525068254 2015-11-03
  • 打赏
  • 举报
回复
引用 3 楼 pps873792861 的回复:
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
这个不行吧 select spmc,spmc1,spmc2 from a 170*1.63,170,1.63 17*1.63,17,1.63 怎么办
西落兮 2015-11-03
  • 打赏
  • 举报
回复
select spmc,substring(spmc1,1,3),substring(spmc2,5,8) from a 
B525068254 2015-11-03
  • 打赏
  • 举报
回复
你这个比较麻烦,我就一个*,比较死的,我希望简单点的,比如split
zbdzjx 2015-11-03
  • 打赏
  • 举报
回复
with table1 as
(
select '170*1.63' col1
)

select col1
, SUBSTRING(col1, 1, patindex('%*%', col1)-1)
, SUBSTRING(col1, patindex('%*%', col1)+1, 100) 
from table1

34,590

社区成员

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

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