使用更新查询增加特定值

时间:2013-12-10 09:34:21

标签: sql-server-2008

select top 1(SUBSTRING(patientid,9,4)) as MaxpatientID from Patient_Data
where PatientID like 'AT112013%' order by PatientID desc;

将结果显示为

MaxPatientID
0014

现在有可能增加这个数字,例如ex:

0015
0016
0017

我尝试了这个查询,但它不值得使用

update Patient_Data  set (top 1(SUBSTRING(patientid,9,4)) as MaxpatientID)=(MaxpatientID + 1)
where PatientID like 'AT112013%' order by PatientID desc;

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

update patientid = SUBSTRING(patientid,0,9)) + convert(varchar(32), convert(int, SUBSTRING(patientid,9,4)) + 1) where patientid like 'AT112013%'

答案 1 :(得分:0)

我刚试过mysql。希望这符合您的要求:

UPDATE test SET patientId = (SELECT patientId
FROM
(
    SELECT CONCAT(SUBSTRING(patientId, 1, 6), CAST(SUBSTRING(patientId, 4, 2) AS UNSIGNED) +1) 
    FROM test
    WHERE `patientId` LIKE 'AT112013%') AS temp
)