数据库中 对原有字段进行字符串追加处理

今日群中一网友 问了一个问题

例如  将数据为 E:\20121129xx.flv

批量改写为 E:\11\20121129xx.flv

 条件为 月份大于0    并且小与13


大体解决思路是这样 

1.截取字符串第8 第9位 字符串 得到月份 "11"

2.拼接字符串成“E:\11\”

concat(concat("E:\\",substring(str,1,2)),"\\")

3.替换开头字符"E:\\" 为“E:\11\”

replace(str,"E:\\",concat(concat("E:\\",substring(str,8,2)),"\\"))

 

 

4.添加条件 条件  因为月份为数字 需要将字符的数字转成数字

  1.比如'0'转成0可以直接用加法来实现

  2.比较数字和varchar时,比如a=11,b="11ddddd";
    则 select 11="11ddddd"相等
    若绝对比较可以这样:
    select 11 = binary "11ddddd"

  3.字符集转换 :   CONVERT(xxx  USING   gb2312)
    类型转换和SQL Server一样,就是类型参数有点点不同  : CAST(xxx  AS   类型)  ,   CONVERT(xxx,类型),类型必须用下列的类型:
    可用的类型    
      二进制,同带binary前缀的效果 : BINARY    
      字符型,可带参数 : CHAR()     
      日期 : DATE     
      时间: TIME     
      日期时间型 : DATETIME     
      浮点数 : DECIMAL      
      整数 : SIGNED     
      无符号整数 : UNSIGNED  

整理条件为

  

where substring(str,1,2) between 0 and 13

 

 

 

最后的语句为 

 

update xxx set str = replace(str,"E:\\",concat(concat("E:\\",substring(str,8,2)),"\\")) where substring(str,1,2) between 0 and 13

 

 

 

 

 

  

  

 

 

posted @ 2012-11-29 17:28  ×jokey  阅读(671)  评论(0编辑  收藏  举报