获取两个字符之间的字符串

时间:2017-05-01 15:28:39

标签: sql sql-server

我有一组字符串:

syms x;

  a=zeros(110,1);
  %stage 3

  v=1; %m/s
  gif=0;
  t=0;
  for i=1:1:110 

  a(i)=(2/50)*(int(x*sin((i*pi*x)/50),x,0,1)+int((2-x)*sin((i*pi*x)/50),x,1,2));
  gif =gif + a(i)*sin((i*pi*(x-v*t)/50));   

  end

  clear x
  figure(1)
  x=[-20 20];
  plot=fplot(gif,x);
  grid on
  xlabel('space');
  ylabel('phi(x)');

目标是返回以下字符串:

String 1:     1*2*3*4*45*
String 2:     1*2*3*4*500*
String 3:     1*2*3*4*5*6*300*

如何使用SQL查询实现此目的?

2 个答案:

答案 0 :(得分:2)

结合使用charindex()stuff()len()reverse()substring()

create table t (str varchar(64));
insert into t values
 ('1*2*3*4*45*')
 ,('1*2*3*4*500*')
 ,('1*2*3*4*5*6*300*');
select 
    str
  , last_value = substring(
       str
      ,1+len(str)-charindex('*',stuff(reverse(str),1,1,''))
      ,charindex('*',stuff(reverse(str),1,1,''))-1)
from t;

rextester演示:http://rextester.com/KYP16744

返回:

+------------------+------------+
|       str        | last_value |
+------------------+------------+
| 1*2*3*4*45*      |         45 |
| 1*2*3*4*500*     |        500 |
| 1*2*3*4*5*6*300* |        300 |
+------------------+------------+

答案 1 :(得分:1)

这是另一种选择。可能会执行相同的SqlZim的精细解决方案。只是另一种方式来拯救同一只猫#34;。

with mycte(SomeVal) as
(
    select '1*2*3*4*45*' union all
    select '1*2*3*4*500*' union all
    select '1*2*3*4*5*6*300*'
)

select *
    , reverse(left(substring(reverse(SomeVal), 2, len(SomeVal)), charindex('*', substring(reverse(SomeVal), 2, len(SomeVal))) - 1))
from mycte
相关问题