需求:实际项目中需要截取第2到第3个逗号中间部分的内容

方案:

declare @str nvarchar(50);
set @str='11,222,3333,44444';
select @str as '字符串'
select len(@str) as '字符长度'
select charindex(',',@str,1) as '第一个逗号的索引值'
select LEFT(@str,charindex(',',@str,1)-1) as '第一个值'
select SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)) as '从第一逗号开始截取出后面的字符串'
select LEFT(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),CHARINDEX(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)-1) as '中间的值'
select SUBSTRING(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),charindex(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)+1,len(@str)) as '最后面的值' --从第二个逗号开始截取出其后的字符串
select LEFT(
SUBSTRING(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),charindex(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)+1,len(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str))))
,CHARINDEX(',',SUBSTRING(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),charindex(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)+1,len(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)))),1)-1
) as '第二中间的值'

SQL截取字符串分隔符中间部门的办法的更多相关文章

  1. 【HANA系列】SAP HANA SQL截取字符串

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL截取字符 ...

  2. 深入SQL截取字符串(substring与patindex)的详解

    首先学习两个函数1.substring  返回字符.binary.text 或 image 表达式的一部分.基本语法:SUBSTRING ( expression , start , length ) ...

  3. SQL截取字符串

    SUBSTRING 返回字符.binary.text      或      image      表达式的一部分.有关可与该函数一起使用的有效      Microsoft®      SQL    ...

  4. sql 截取字符串与 截取字符串最长的字符串

    ); set @str='aa,32,22,55,7'; ) as '第一个逗号的索引值' )),),),'') as '第一个值' ),len(@str)) as '从第一逗号开始截取出后面的字符串 ...

  5. SQL截取字符串函数

    A.截取从字符串左边开始N个字符 以下是代码片段:    Declare @S1 varchar(100)  Select @S1='http://www.xrss.cn'  Select Left( ...

  6. SQL 截取字符串

    以下代码演示的是除去字符串后四位 ,) 下面是SUBSTRING的解释 SUBSTRING ( expression ,start , length ) 第一个参数是字符串,第二个是起始位置,第三个是 ...

  7. sql 截取字符串第一次出现字符之前的数据

    截取sql 第一次出现字符之前的数据  (select left( a.ChangeProductName,charindex(',', ChangeProductName)-1)) as Chang ...

  8. sql截取字符串后面四位

    方法1: select substr('123456789',length('123456789')-6+1,6) from dual; 方法2: SELECT name, RIGHT(certid, ...

  9. SQL Server中截取字符串常用函数

    SQL Server 中截取字符串常用的函数: .LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要截 ...

随机推荐

  1. Emulating private methods with closures

    [Emulating private methods with closures] JavaScript does not provide a native way of doing this, bu ...

  2. new.target

    [new.target] The new.target property lets you detect whether a function or constructor was called us ...

  3. webservice客户端 get delete post 请求

    package com.cn.eport.util.common; import java.io.IOException; import java.util.List; import org.apac ...

  4. python+selenium的环境配置

    以前写过关于python和selenium加myeclipse的环境配置,但是myeclipse启动时过于费时,虽然myeclipse有很好的提示功能,但是作为初学者,我还是直接用python的idl ...

  5. tomcat指向外部项目

    参考 https://www.cnblogs.com/ysocean/p/6893446.html conf/server.xml中增加 <Context path="/myweb&q ...

  6. 第十章 优先级队列 (b2)完全二叉堆:插入与上滤

  7. leetcode题库解答源码(python3)

    下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...

  8. pandas中series和dataframe之间的区别

    series结构有索引,和列名组成,如果没有,那么程序会自动赋名为None series的索引名具有唯一性,索引可以数字和字符,系统会自动将他们转化为一个类型object. dataframe由索引和 ...

  9. 约束布局 ConstraintLayout

    app:layout_constraintVertical_bias="0.5"app:layout_constraintHorizontal_bias="0.5&quo ...

  10. pep8 && pep20

    pep8(部分,遇到问题再补充) 1.不建议使用tab键来空格,避免不必要的空格.操作符左右各加一个空格,函数默认参数使用的赋值符左右省略空格. 2.类和top-level函数定义之间空两行:类中的方 ...