一个表
id (int)      path(varchar)
---------  ------------
1              c:\aa\dd1\ee_i.jpg
2              c:\bbb\dd21\ff_i.jpg
3              c:\cccc\dd31\mm_i.jpg
4              c:\dddd\dd4\nn_i.jpg
要求一个语句以后变成
id (int)      path(varchar)
---------  ------------
1              d:\1_ee_i.jpg
2              d:\2_ff_i.jpg
3              d:\3_mm_i.jpg
4              d:\4_nn_i.jpg即path = 'd:\' + id + '_' + 原文件名大概有10w条这样的记录要处理请教sql语句,并且,问一下,哪里有关于sql里这些常用基本函数的说明?例如convert...之类的,还有那些处理字符串的

解决方案 »

  1.   


    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([id] int,[path] varchar(21))
    insert [TB]
    select 1,'c:\aa\dd1\ee_i.jpg' union all
    select 2,'c:\bbb\dd21\ff_i.jpg' union all
    select 3,'c:\cccc\dd31\mm_i.jpg' union all
    select 4,'c:\dddd\dd4\nn_i.jpg'select id,
    [path]='d:\'+rtrim(id)+'_'+right(path,charindex('\',reverse(path))-1) from [TB]/*
    id          path
    ----------- -------------------------------------
    1           d:\1_ee_i.jpg
    2           d:\2_ff_i.jpg
    3           d:\3_mm_i.jpg
    4           d:\4_nn_i.jpg(4 行受影响)*/drop table [TB]
      

  2.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[path] varchar(21))
    insert [tb]
    select 1,'c:\aa\dd1\ee_i.jpg' union all
    select 2,'c:\bbb\dd21\ff_i.jpg' union all
    select 3,'c:\cccc\dd31\mm_i.jpg' union all
    select 4,'c:\dddd\dd4\nn_i.jpg'
    goupdate tb
    set path='d:\' + ltrim(id) + '_' + right(path,charindex('\',reverse(path))-1)select * from tb
    /**
    id          path
    ----------- ---------------------
    1           d:\1_ee_i.jpg
    2           d:\2_ff_i.jpg
    3           d:\3_mm_i.jpg
    4           d:\4_nn_i.jpg(4 行受影响)**/
      

  3.   

    我原来也是这样写的,但是我是些的str(id),因为id是整数
    2,3楼都是用*trim(id),是效率上有差异么?
    我原来就是怕我写的效率太低,所以来问问,现在没有大量的数据来测试
      

  4.   

    用str默認是10個字符,str(10)
    LTRIM(STR([id]))--需要加多一層取掉空格效率沒有明顯差異,個人建議這樣的情況用STR有點多餘
      

  5.   


    CONVERT(id AS varchar(10) --显式转换int为varchar
    *TRIM(id)                 --隐式转换int为varchar,再去掉一侧空格
    STR(id)                   --隐式转换int为float,再转换为varchar个人感觉,第1种性能最好,第2种写起来最方便。第3种除非是处理float数值,否则不要使用。
      

  6.   


    declare @table table (id int,path varchar(21))
    insert into @table
    select 1,'c:\aa\dd1\ee_i.jpg' union all
    select 2,'c:\bbb\dd21\ff_i.jpg' union all
    select 3,'c:\cccc\dd31\mm_i.jpg' union all
    select 4,'c:\dddd\dd4\nn_i.jpg'select id,'d:\'+cast(id as varchar(8))+'_'+right(path,charindex('\',reverse(path))-1) 
    as [path] from @table
    /*
    id          path
    ----------- ---------------------------------
    1           d:\1_ee_i.jpg
    2           d:\2_ff_i.jpg
    3           d:\3_mm_i.jpg
    4           d:\4_nn_i.jpg
    */