数据库字段数据修改问题

wangxuebo 2012-08-01 11:33:34
数据库字段 pic
里面的数据有
20054181241305.jpg
/productFiles/2007-10/20071012154826320.gif
怎么把里面的的数据改为这样的格式啊
/productFiles/20054181241305.jpg
/productFiles/20071012154826320.gif

就是加上路径,去掉日期
怎么用SQL语句修改。。
...全文
157 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2012-08-01
  • 打赏
  • 举报
回复

create table wan(pic varchar(100))

insert into wan
select '20054181241305.jpg' union all
select '/productFiles/2007-10/20071012154826320.gif'


update wan set pic='/productFiles/'
+reverse(substring(reverse(pic),1,len(pic)-charindex('/',reverse(pic),1)))

select * from wan
/*
pic
---------------------------------------
/productFiles/20054181241305.jpg
/productFiles/20071012154826320.gif

(2 row(s) affected)
*/
  • 打赏
  • 举报
回复
#1楼写的很清楚了,可以实现你的要求,
select '/productFiles/'
+reverse(substring(reverse(pic),1,len(pic)-charindex('/',reverse(pic),1))) 'pic'
from wan

wangxuebo 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code


create table wan(pic varchar(100))

insert into wan
select '20054181241305.jpg' union all
select '/productFiles/2007-10/20071012154826320.gif'


select '/productFiles/'
+reverse(……
[/Quote]大哥,本人是菜鸟,你写的我看不懂啊。
我的意思是数据库有个字段叫pic
里面保存的数据格式主要是下面两种,还有一些其他的就不管了
20054181241305.jpg
/productFiles/2007-10/20071012154826320.gif
现在我要把这两种格式的数据都改为下面这样的格式的
/productFiles/20054181241305.jpg
/productFiles/20071012154826320.gif
就是没文件夹路径的就加上文件夹,有日期格式的,就去掉日期
唐诗三百首 2012-08-01
  • 打赏
  • 举报
回复

create table wan(pic varchar(100))

insert into wan
select '20054181241305.jpg' union all
select '/productFiles/2007-10/20071012154826320.gif'


select '/productFiles/'
+reverse(substring(reverse(pic),1,len(pic)-charindex('/',reverse(pic),1))) 'pic'
from wan

/*
pic
---------------------------------------
/productFiles/20054181241305.jpg
/productFiles/20071012154826320.gif

(2 row(s) affected)
*/
wangxuebo 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
SQL code

create table ta(pic varchar(128))
insert ta
select '20054181241305.jpg'
union select '/productFiles/2007-10/20071012154826320.gif'

select * from ta
/*
pic
20054181241305.jpg
/……
[/Quote]
update ta set pic =case when CHARINDEX('/',PrdPic1,0)=0 then 'productFiles/'+PrdPic1  else 'productFiles/'+reverse(substring(reverse(PrdPic1),1,charindex('/',reverse(PrdPic1),1)-1))
这个有错误,运行不了
Felixzhaowenzhong 2012-08-01
  • 打赏
  • 举报
回复
create table ta(pic varchar(128))
insert ta
select '20054181241305.jpg'
union select '/productFiles/2007-10/20071012154826320.gif'

select * from ta
/*
pic
20054181241305.jpg
/productFiles/2007-10/20071012154826320.gif
*/
update ta set pic =case when CHARINDEX('/',pic,0)=0 then '/productFiles/'+pic else '/productFiles/'+reverse(substring(reverse(pic),1,charindex('/',reverse(pic),1)-1))
end

/*
pic
/productFiles/20054181241305.jpg
/productFiles/20071012154826320.gif
*/
drop table ta
唐诗三百首 2012-08-01
  • 打赏
  • 举报
回复
楼主的pic字段的值都有哪些类型格式的,列出几个来.
wangxuebo 2012-08-01
  • 打赏
  • 举报
回复
update Cproduct set pic='/productFiles/'
+reverse(substring(reverse(pic),1,len(pic)-charindex('/',reverse(pic),1)))
我把这句拿到数据库里面执行了,报错
消息 8152,级别 16,状态 14,第 1 行
将截断字符串或二进制数据。
语句已终止。
是不是字段里面有其他格式或有空值的原因啊?
孤独加百列 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

SQL code

create table wan(pic varchar(100))

insert into wan
select '20054181241305.jpg' union all
select '/productFiles/2007-10/20071012154826320.gif'


update wan set pic='/productFiles/'
+revers……
[/Quote]
改成你的表结构列名就可以了
wangxuebo 2012-08-01
  • 打赏
  • 举报
回复

create table wan(pic varchar(100))

insert into wan
select '20054181241305.jpg' union all
select '/productFiles/2007-10/20071012154826320.gif'


update wan set pic='/productFiles/'
+reverse(substring(reverse(pic),1,len(pic)-charindex('/',reverse(pic),1)))

select * from wan

是不是把这段拿到查询分析器里面执行一下就可以了?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧