更新sql server中应增加1的字符串值列

时间:2013-08-19 11:32:01

标签: sql sql-server

在我的数据库中,我的列包含40个名为jee_phy_2的相同记录。我希望将数字增加1,例如jee_phy_2jee_phy_40。任何人都可以告诉我

DECLARE @q_id  NVARCHAR(50),@txt bigint,@final nvarchar(50)
begin 

SET @q_id='JEE_PHY_1' 


UPDATE oems_question7 SET q_id=@q_id+1 WHERE id between 1 and 40

end

2 个答案:

答案 0 :(得分:1)

这是你在寻找什么?

UPDATE x
SET x.q_id = 'jee_phy_' + cast(x.newNum as nvarchar(10))
FROM (
      SELECT q_id, ROW_NUMBER() OVER (ORDER BY [q_id]) AS newNum
      FROM oems_question7
      ) x

fiddle demo

答案 1 :(得分:0)

试试这个

DECLARE @q_id  NVARCHAR(50)
begin 

SET @q_id='JEE_PHY_1' 

UPDATE oems_question7  SET q_id = SUBSTRING (@q_id ,0,CHARINDEX('_', @q_id,5) + 1 ) + ' ' + CONVERT(NVARCHAR,CONVERT(INT, SUBSTRING ( @q_id ,CHARINDEX('_', @q_id,5) + 1, LEN(@q_id) )) +1)
WHERE id between 1 and 40

end