更新表中特定的字段

例:我有一张合同表Contract

Id Name Total buget
1 合同名称 100 102,22
2 合同名称2 300 ,102,22,


现在我想更新表中buget字段,更新条件为:如果表buget中值前后没有","要加上",",也就是说将第一行的buget值更新为",102,22,"的样子,没有where条件的。
目的是更新旧数据库中所有老数据,所以有个判断,就是前面没有","的值要加上",".此条件如何用sql语句来编写出来?谢谢大侠们!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

同时小弟还想请问社区强人怎么设计数据库比较合理,一张表中多少个字段为易?
个人在编程中始终在想如何编写能提高运行效率,在此更渴望能结交师傅朋友们!个人联系方式:QQ:648155643 万圣感激!

白衣染霜花
浏览 586回答 7
7回答

梦里花落0921

update [Contract] set Buget=',' Buget where left(Buget,1)<>',';   update [Contract] set Buget=Buget ',' where right(Buget,1)<>',';   表中字段不一定,曾经有个大表,80 多个字段 字段最好不好超过40个,另外这个也看字段的类型,如果都是int 或者 bit 类型的字段,多一些问题也不大 个人意见

HUWWW

IF NOT OBJECT_ID('[Contract]') IS NULL DROP TABLE [Contract]GO Create table [Contract] (ID int primary key identity(1,1) ,[Name] nvarchar(50) null ,Total float null ,buget Nvarchar(500) null ) go insert into [Contract]select '合同名称', 100,'102,22'union all select '合同名称2', 300,',102,22,'union all select '合同名称3', 300,'101,23,'--------update [Contract] set buget=',' buget where left(buget,1)=','--------update [Contract] set buget=buget ',' where right(buget,1)=','update [Contract] set buget=(case when (left(buget,1)!=',' and right(buget,1)!=',') then ',' buget ',' when left(buget,1)!=',' then ',' buget when right(buget,1)!=',' then buget ',' else buget end)/*(3 row(s) affected)*/select * from [Contract]

侃侃无极

谢谢各位大侠们,此问题我已解决了,谢谢你们!分数我来给大家平分吧。谢谢你们!

绝地无双

create table aa( buget varchar(50)) update aa set buget=',' buget ',' where  left(buget,1)<>',' or right(buget,1)<>',' select * from aa

繁花如伊

update aa set buget=',' buget ',' where left(buget,1)',' or right(buget,1)','忘记了 号

HUX布斯

不知道为什么加号打不上去

哆啦的时光机

谢谢各位大侠们,此问题我已解决了,谢谢你们!分数我来给大家平分吧。谢谢你们!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server