Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

1 solution

Hi,

Try This....

SQL
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
 
Share this answer
 
v3
Comments
PRAKASH_N 15-Mar-13 2:10am    
it has more than 10 val().....
gvprabu 15-Mar-13 2:11am    
all val() in same row ?
gvprabu 15-Mar-13 2:13am    
If all the val() in same row, then go for Loop, same logic you can use.... up to all val() replaced ... :-)
PRAKASH_N 15-Mar-13 2:28am    
val() in same row and in next rows also......
gvprabu 15-Mar-13 3:14am    
Ok, then go for loop. how many rows are there?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900