成都企业做网站多少钱,视频网站开发背景,wordpress主题logo大小,沈阳网 沈阳网站字符串截断函数是指#xff1a;Stuff 和 SubString#xff0c;字符串查找函数是#xff1a;CharIndex 和 PatIndex 一#xff0c;SubString 截取子串 最常用的字符串函数#xff0c;用于截取特定长度的子串。 SUBSTRING ( expression ,start , length ) 参数说明#xff… 字符串截断函数是指Stuff 和 SubString字符串查找函数是CharIndex 和 PatIndex 一SubString 截取子串 最常用的字符串函数用于截取特定长度的子串。 SUBSTRING ( expression ,start , length ) 参数说明 start 参数整数表示开始位置字符的序号index从1开始即第一个字符的序号是1length参数整数表示截取字符的最大数量如果startLength 大于字符串的总长度那么返回从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或大于字符串的总长度那么该函数返回NULLlength整数表示删除字符的最大数量如果LengthStart大于字符串的总长度表示删除从Start开始的所有字符replaceWith_expression 字符类型表示从开始位置start插入的字符串 例如从不同位置截断字符串abcdef查看返回结果 declare str varchar(6)
set strabcdefselect stuff(str,7,1,),stuff(str,0,1,),stuff(str,3,7,),stuff(str,3,7,12345) 使用stuff函数必须注意两点 如果LengthStart大于字符串的总长度删除从Start开始的所有字符如果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 dt12015-01-02 01:23:45.678
set dt22015-01-02 01:23:45.000select 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 dt12015-01-02 01:23:45.678
set dt22015-01-02 01:23:45.000select 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) --业精于勤而荒于嬉行成于思而毁于随-- --欢迎转载转载请注明出处--