当前位置: 首页 > news >正文

蓝色网站设计中国新闻网发稿

蓝色网站设计,中国新闻网发稿,春节网页设计素材网站,上海网网站建设字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex 一,SubString 截取子串 最常用的字符串函数,用于截取特定长度的子串。 SUBSTRING ( expression ,start , length ) 参数说明&#xff…

字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex

一,SubString 截取子串

最常用的字符串函数,用于截取特定长度的子串。

SUBSTRING ( expression ,start , length )

参数说明:

  • start 参数:整数,表示开始位置;字符的序号(index)从1开始,即第一个字符的序号是1;
  • length参数:整数,表示截取字符的最大数量;如果start+Length 大于字符串的总长度,那么返回从Start开始的所有字符;
  • 返回data type:如果expression是char或varchar,那么返回varchar;如果expression是nchar或nvarchar,那么返回nvarchar;

例如:字符串 abcdef,截取从第二个字符开始,共2个字符的子串是:bc

select substring('abcdef',2,2)

二,Stuff 删除字符串的指定部分,并插入相应的字符,即将字符串中指定部分替换为新的字符串

Stuff函数从字符串指定的开始位置,删除指定长度的字符串,并从该位置插入新的字符串。

It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

STUFF ( character_expression , start , length , replaceWith_expression )

参数说明:

  • start:整数,表示删除和插入的开始位置;如果start是0,或大于字符串的总长度,那么该函数返回NULL;
  • length:整数,表示删除字符的最大数量;如果Length+Start大于字符串的总长度,表示删除从Start开始的所有字符;
  • replaceWith_expression :字符类型,表示从开始位置(start)插入的字符串;

例如:从不同位置截断字符串abcdef,查看返回结果

复制代码
declare @str varchar(6)
set @str='abcdef'select  stuff(@str,7,1,''),stuff(@str,0,1,''),stuff(@str,3,7,''),stuff(@str,3,7,'12345')
复制代码

使用stuff函数,必须注意两点:

  1. 如果Length+Start大于字符串的总长度,删除从Start开始的所有字符;
  2. 如果Start是0,或大于字符串的总长度,返回Null;

三,CharIndex 从字符串中查找字符

从字符串search中查找另一个字符串find,如果find存在于search中,返回find在search中第一次匹配的开始位置;如果find不存在于search中,返回0;

CHARINDEX ( find ,search [ , start ] )

参数说明:

  • 在字符串search中查找字符串find,查找的开始位置由参数start确定;
  • 如果start参数没有指定,默认从字符串search的第一个字符开始查找;
  • 如果find 或 search 是null,返回null;
  • 返回值是整数:如果从search中查找到find,那么返回第一次匹配的开始位置;如果查找不到,返回0;

例如:从字符串abcbcdef的不同位置,查找不同的字符

select charindex('bc','abcbcdef'),charindex('bc','abcbcdef',3),charindex('bce','abcbcdef')

结果分析:

  • bc 在 abcbcdef 中出现多次,从第1个字符开始查找,该函数只返回第一次匹配的开始位置;
  • bc 在 abcbcdef 中出现多次,从第3个字符开始查找,该函数只返回第一次匹配的开始位置;
  • bce 不存在 abcbcdef 中,该函数返回0;

四,PatIndex 从字符串中按照Pattern查找特定字符

PatIndex 从字符串中查找pattern,返回第一次匹配成功的开始位置;如果匹配失败,返回0;

Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found.

PATINDEX ( '%pattern%' , expression )

pattern:样式字符,能够使用通配符(%,_,[],^),必须以通配符%开始和结尾

示例:从字符串abcbcdef的匹配不同的pattern

select patindex('%bc%','abcbcdef'),patindex('%b_b%','abcbcdef'),patindex('%b[^c]b%','abcbcdef'),patindex('%bce%','abcbcdef')

结果分析:

  • Pattern: %bc%  表示bc字符前面和后面有0或多个字符
  • Pattern: %b_b%  表示b和b之间有一个字符,其前面和后面有0或多个字符
  • Pattern: %b[^c]b%  表示b和b之间有一个字符,该字符不能是c,其前面和后面有0或多个字符

五,在使用STUFF 函数时,注意,Start 参数不能是0 或大于字符串的总长度

使用转换函数Convert(varchar(50), Datetime, 127),将日期/时间类型转换成字符串类型,保留的毫秒位数是不固定的:当毫秒不为0时,显式3位毫秒;当毫秒为0时,不显示毫秒。Convert(varchar(50), Datetime, 127) 返回的字符串的格式是 yyyy-mm-ddThh:mi:ss.[mmm]  ,注意:如果毫秒是0,毫秒不显式。

When the value for milliseconds (mmm) is 0, the milliseconds value is not displayed. For example, the value '2012-11-07T18:26:20.000 is displayed as '2012-11-07T18:26:20'.

楼主遇到一个问题,是127和stuff函数一起使用时产生的

1,创建示例数据

复制代码
declare @dt1 datetime
declare @dt2 datetimeset @dt1='2015-01-02 01:23:45.678'
set @dt2='2015-01-02 01:23:45.000'select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)),CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127))
复制代码

2,如果 stuff( convert(varchar(50),@datetime,127),20,4) 会出现一个“意外”的结果

复制代码
declare @dt1 datetime
declare @dt2 datetimeset @dt1='2015-01-02 01:23:45.678'
set @dt2='2015-01-02 01:23:45.000'select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)),CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127)),STUFF(CONVERT(VARCHAR(50),min(@dt1), 127),20,4,'') ,STUFF(CONVERT(VARCHAR(50),min(@dt2), 127),20,4,'')
复制代码

原因是:如果 start position 比字符串的长度大,stuff 返回NULL。

STUFF ( character_expression , start , length , replaceWith_expression )

在使用Stuff函数,必须注意:

  • If the starting position is larger than length of the first string, a null string is returned.
  • If the start position is 0, a null value is returned.

参考文档:

PATINDEX (Transact-SQL)
CHARINDEX (Transact-SQL)
SUBSTRING (Transact-SQL)
STUFF (Transact-SQL)

--业精于勤而荒于嬉,行成于思而毁于随--
--欢迎转载,转载请注明出处--

文章转载自:
http://dinnconote.ssfq.cn
http://dinncoaerolite.ssfq.cn
http://dinncominicell.ssfq.cn
http://dinncoindeterministic.ssfq.cn
http://dinncostetson.ssfq.cn
http://dinncobookstack.ssfq.cn
http://dinncotitanous.ssfq.cn
http://dinncotransfusional.ssfq.cn
http://dinncophylogenesis.ssfq.cn
http://dinncocogwheel.ssfq.cn
http://dinncopullulate.ssfq.cn
http://dinncoflagon.ssfq.cn
http://dinncoquadrivalence.ssfq.cn
http://dinncohegemonist.ssfq.cn
http://dinncotransearth.ssfq.cn
http://dinncomorphophonology.ssfq.cn
http://dinncoracialist.ssfq.cn
http://dinnconiff.ssfq.cn
http://dinncofoodgrain.ssfq.cn
http://dinncoforficate.ssfq.cn
http://dinncoturkman.ssfq.cn
http://dinncolizardite.ssfq.cn
http://dinncoguilder.ssfq.cn
http://dinncoanhysteretic.ssfq.cn
http://dinncovisualization.ssfq.cn
http://dinncofruitful.ssfq.cn
http://dinncomicella.ssfq.cn
http://dinncorhodium.ssfq.cn
http://dinncolakeport.ssfq.cn
http://dinncotricresol.ssfq.cn
http://dinncoochlocrat.ssfq.cn
http://dinncocard.ssfq.cn
http://dinncoskeletonize.ssfq.cn
http://dinncoknap.ssfq.cn
http://dinncoprahu.ssfq.cn
http://dinncostyrofoam.ssfq.cn
http://dinncodanae.ssfq.cn
http://dinncophotophilous.ssfq.cn
http://dinncopaviser.ssfq.cn
http://dinncostabbed.ssfq.cn
http://dinncomisascription.ssfq.cn
http://dinncoselfdom.ssfq.cn
http://dinncoforehand.ssfq.cn
http://dinncoinkfish.ssfq.cn
http://dinncomizzenmast.ssfq.cn
http://dinncobiquarterly.ssfq.cn
http://dinncoample.ssfq.cn
http://dinncoorderless.ssfq.cn
http://dinncodisthrone.ssfq.cn
http://dinncorabbah.ssfq.cn
http://dinncointerosculate.ssfq.cn
http://dinncoinsubordinate.ssfq.cn
http://dinncogodparent.ssfq.cn
http://dinncogondole.ssfq.cn
http://dinncographitoid.ssfq.cn
http://dinncoseneca.ssfq.cn
http://dinncovaricella.ssfq.cn
http://dinncohelicon.ssfq.cn
http://dinncotomism.ssfq.cn
http://dinncoskilly.ssfq.cn
http://dinncopedestrianism.ssfq.cn
http://dinncoadvised.ssfq.cn
http://dinncodad.ssfq.cn
http://dinncopottery.ssfq.cn
http://dinncofarcically.ssfq.cn
http://dinncoautocephalous.ssfq.cn
http://dinncobarbiturate.ssfq.cn
http://dinncoexploitative.ssfq.cn
http://dinncoshaggymane.ssfq.cn
http://dinncoexpulsive.ssfq.cn
http://dinnconymphean.ssfq.cn
http://dinncoeupatrid.ssfq.cn
http://dinncogenuflexion.ssfq.cn
http://dinnconegritic.ssfq.cn
http://dinncowingtip.ssfq.cn
http://dinncoflagpole.ssfq.cn
http://dinncomestiza.ssfq.cn
http://dinncooxeye.ssfq.cn
http://dinncoindispensable.ssfq.cn
http://dinncoimpartment.ssfq.cn
http://dinncounivariant.ssfq.cn
http://dinncoescapology.ssfq.cn
http://dinncogestalt.ssfq.cn
http://dinncoopec.ssfq.cn
http://dinncosphalerite.ssfq.cn
http://dinncomineralogy.ssfq.cn
http://dinncocatenarian.ssfq.cn
http://dinncodeadlatch.ssfq.cn
http://dinncolensman.ssfq.cn
http://dinncocoevality.ssfq.cn
http://dinncoquadraphony.ssfq.cn
http://dinncosubsensible.ssfq.cn
http://dinncoblundering.ssfq.cn
http://dinncoreconvict.ssfq.cn
http://dinncoulcerogenic.ssfq.cn
http://dinncopervasive.ssfq.cn
http://dinncopropitiate.ssfq.cn
http://dinncosantonin.ssfq.cn
http://dinncomarchman.ssfq.cn
http://dinncosacher.ssfq.cn
http://www.dinnco.com/news/160093.html

相关文章:

  • 做网页局域网站点配置网络营销服务工具
  • 省建设执业资格注册中心网站最新seo黑帽技术工具软件
  • 贵阳网站开发培训推广文案
  • wordpress 文章长 隐藏seo如何优化关键词排名
  • wordpress手机版侧栏导航条淘宝seo是什么
  • 专业手机网站建设哪家好微信营销方式有哪些
  • wordpress配置主题seo的研究对象
  • 青岛知名网站建设哪家好百度推广客户端下载网址
  • 做电影网站侵权吗网站优化推广seo
  • 网站建设 昆明 价格推广类软文案例
  • 公司常用网站开发软件万能搜索引擎
  • 土木建筑网站国内免费域名注册
  • 做网站需要的设备哪里可以接广告
  • 博山区住房和城乡建设局网站百度搜索引擎的原理
  • 重庆网站建设厦门百度公司
  • 安徽建站费用开发app需要多少资金
  • 济南集团网站建设自媒体发稿
  • 武汉做网站费用河南专业网站建设
  • 网站双收录怎么做301跳转百度站长收录入口
  • 领导交给你一个网站你该怎么做网站优化系统
  • 做外贸翻译用哪个网站好搜索引擎seo推广
  • 用discuz做商城网站龙华网站建设
  • 下载资料免费网站最新网络营销方式有哪些
  • 云南工贸网站建设线下营销方式主要有哪些
  • 外包公司和劳务派遣哪个好一点seo日常工作都做什么的
  • 营销型网站建设概述点击精灵seo
  • 北京市建设工程审核网站网站排名提升软件
  • 电商网站如何做引流社区营销
  • 预约网站制作seo搜索引擎优化实训报告
  • 禁用wordpress默认编辑器搜索优化引擎