从SQL Server更新列中的字符串或单词 [英] updating a string or a word from a column from SQL Server

查看:67
本文介绍了从SQL Server更新列中的字符串或单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







i有一张叫做testtb的桌子



在testtb表中,id和desc是两列。





id desc



1 val(5679)来自cdetail



2 val(idsk)来自外地







现在我想使用SQL以强制转换(5679为int)替换 val(5679)。下一行同样的事情,直到找到val()。



请让我知道一些建议

解决方案





试一下....



  DECLARE   @ Test   TABLE (id  int  ,[ desc ]  varchar  1000 ) )
DECLARE @ ReplaceStr VARCHAR 30 )= ' cast(5679 as int) '

INSERT INTO @ Test (id,[ desc ])
VALUES 1 ,< span class =code-string>' val(5679)来自cdetail'),( 2 ' val(idsk)from exide'

< span class =code-keyword> SELECT id,[ desc ],
REPLACE([ desc ],(SUBSTRING([ desc ],CHARINDEX(' val(',[ desc ], 1 ),( CHARINDEX(' )',[ desc ], 1 ) - CHARINDEX(' val(',[ desc ], 1 ))+ 1)), @ ReplaceStr ' RequiredDesc'
FROM @ test



- 注意:如果只有一个val()那么它只能起作用:-)

问候,

GVPrabu


Hi,


i have a table called testtb

In testtb table, id and desc are two the columns.


id desc

1 val(5679) from cdetail

2 val(idsk) from exide



Now i want to replace the val(5679) with cast(5679 as int) progmatically using SQL. same thing for the next row until it finds val().

Please let me know some suggetsions

解决方案

Hi,

Try This....

DECLARE @Test TABLE(id int, [desc] varchar(1000))
DECLARE @ReplaceStr VARCHAR(30)='cast(5679 as int)'

INSERT INTO @Test(id,[desc])
VALUES(1, ' val(5679) from cdetail'),(2, ' val(idsk) from exide ')

SELECT id, [desc], 
    REPLACE([desc],(SUBSTRING([desc],CHARINDEX('val(',[desc],1),(CHARINDEX(')',[desc],1)-CHARINDEX('val(',[desc],1))+1)),@ReplaceStr) 'RequiredDesc'
FROM @test


-- Note : If only one val() then only it will work :-)
Regards,
GVPrabu


这篇关于从SQL Server更新列中的字符串或单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆