一、概述:

MSSQL字符串的拆分没有封装太多常用的方式,所以如果向数据库中插入用特殊字符分割字符串(比如CB0$CB2$CB3,CB0$CB2$CB3)时就可能需要数据库能够分割字符串,SQL中拆分字符串的常用方法有

1、len(@strname) :查询字符串的长度

2、charindex('$',@strname,1) :查询特殊字符存在的位置index

3、substring(@strname,startindex,length):截取字符串长度

4、left(@strname,length):左边截取length字符

5、right(@strname,length):右边截取length字符

二、字符串动态拆分

现在完成一个查询,要求:动态输入格式CB1,CB2,CB3类似的字符串,字符串中间有个逗号分隔符,用字符串拆分,动态分割出所有组合字符串(逗号左右字符串);

实例1:

declare @PowerString nvarchar()
set @PowerString='CB0$CB2$CB3,CB0$CB2$CB3'
declare @startindex int
declare @endindex int
declare @CurrResult nvarchar() set @startindex=
set @endindex=
while =
begin
set @endindex=charindex(',',@PowerString,@startindex);
if @endindex=
set @endindex=LEN(@PowerString)+
set @CurrResult=SUBSTRING(@PowerString,@startindex,@endindex-@startindex)
select @CurrResult
set @startindex=@endindex+
if @CurrResult is null or @CurrResult='' or @endindex=LEN(@PowerString)+
break;
end

实例二:一个存储过程,通过字符串拆分加上事务绑定,能够保证数据的完整性

Alter Proc p_HotelTour_Create
@HotelCode nvarchar(),
@TouPics nvarchar(),
@resultcode int output
as
begin
declare @startindex int
declare @endindex int
declare @CurrResult nvarchar()
set @startindex=
set @endindex=
begin tran
begin try
--插入内容图片
while =
begin
set @endindex=charindex('$',@TouPics,@startindex);
if @endindex=
set @endindex=LEN(@TouPics)+
set @CurrResult=SUBSTRING(@TouPics,@startindex,@endindex-@startindex)
if not exists(select c_TourCode from HotelTour where c_HotelCode=@HotelCode and c_TourCode=@CurrResult)
insert into HotelTour(c_HotelCode,c_TourCode,i_IsEnable) values(@HotelCode,@CurrResult,)
set @startindex=@endindex+
if @CurrResult is null or @CurrResult='' or @endindex=LEN(@TouPics)+
break;
end
commit tran
set @resultcode=
end try
begin catch
rollback tran
set @resultcode=-
end catch
end

SQL数据字符串的拆分的更多相关文章

  1. sql 拼接字符串单条拆分多条

    SELECT * FROM ( SELECT A.WS_ID , B.NEXT_OPERATOR FROM ( SELECT WS_ID , [NEXT_OPERATOR] = CONVERT(XML ...

  2. sql 把一个用逗号分隔的多个数据字符串变成一个表的一列

    USE [tms]GO/****** Object: UserDefinedFunction [dbo].[StrToTable] Script Date: 2017/4/26 9:06:20 *** ...

  3. MSSQL Server数据库的四种连接方法和sql连接字符串

    MSSQL Server数据库的四种连接方法和sql连接字符串 分类: [ 03 ] C#(131) [ 07 ] SQL Server(68) [ 01 ] .NET(189) 今天用SQL Ser ...

  4. SQL数据库面试题

    SQL数据库面试题 1.SQL SREVER中,向一个表中插入了新数据,如何快捷的得到自增量字段的当前值 这种情况其实我们经常用到,比如我们新建了一个用户,建立完用户后我们希望马上得到这个新用户的ID ...

  5. sql数据黑马程序员——SQL入门

    最近研究sql数据,稍微总结一下,以后继续补充: ---------------------- ASP.Net+Android+IO开辟S..Net培训.等待与您交流! --------------- ...

  6. SQL数据缓存依赖总结

    以前只听过SQL server数据缓存依赖,但一直没使用,由于项目需要,才研究了一番,发现了一个很诡异的问题,竟然是一个操作顺序问题导致的. SQL server数据缓存依赖有两种实现模式,轮询模式, ...

  7. SQL数据查询之——单表查询

    一.SQL数据查询的一般格式 数据查询是数据库的核心操作.SQL提供了SELECT语句进行数据查询,其一般格式为: SELECT [ALL | DISTINCT]<目标列表达式>[,< ...

  8. sql server字符串的类型

    sql server字符串分为不同的类型,下面就将为您介绍几个sql server字符串的常见类型,希望对您学习sql server字符串能有所帮助. nchar 此数据类型可存储1~4000个定长U ...

  9. SQL中字符串截取、连接、替换等函数的用法

    一.SQL中SUBSTRING函数的用法1.功能:返回字符.二进制.文本或图像表达式的一部分2.语法:SUBSTRING ( expression, start, length )3.QL 中的 su ...

随机推荐

  1. OpenCV图像Canny边缘检测

    Canny边缘检测 图像的边缘检测的原理是检测出图像中所有灰度值变化较大的点,而且这些点连接起来就构成了若干线条,这些线条就可以称为图像的边缘函数原型:     void cvCanny(       ...

  2. linux下使用ffmpeg将amr转成mp3

    说明:AMR格式是智能手机上的常用音频文件格式,比MP3格式的压缩比大.同样时长的AMR文件大概是MP3的十分之一,所以在移动互联项目中应用比较广泛.但目前AMR格式在个人电脑上应用较少,所以目前大部 ...

  3. 转:bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  4. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  5. Python tab键自动补齐

    1.进入root家目录  建立.tab文件 .tab文件内容如下: ############################################## import sys import r ...

  6. PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

    /*常用的字符串输出函数 * * echo() 输出字符串 * print() 输出一个或多个字符串 * die() 输出一条信息,并退出当前脚本 * printf() 输出格式化字符串 * spri ...

  7. jQuery清除文本框,内容并设置不可用

    JQuery清除文本框,内容并设置不可用  如果是设置只读,则将disabled换成readonly function CleanText(textid) { $("#"+text ...

  8. POJ 2051

    http://poj.org/problem?id=2051 这个题目的大题意思就是给你一些ID,和ID所对应的周期,每隔它所对应的周期,它的任务就会执行,就会输出所对应的ID Register 20 ...

  9. Oracle java.sql.SQLException: 数字溢出

    六月 30, 2016 5:47:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinit ...

  10. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...