[英]how to update a column with same sequence
i need to update a table with another column (userID ) to Lastname Column .So i have written query like this 我需要将具有另一列(userID)的表更新为Lastname Column。所以我已经编写了这样的查询
update User set LastName = +'LastName_'+ CONVERT (varchar(10),UserID)
and result giving like this 结果像这样
UserID FirstName LastName
1 AALIYAH Bhatt_1
2 Mohan Kumar_2
3 varun ratna_3
4 suresh rania_4
5 AARON suresh_5
etc ......
4500 Kalyan raju_4500
4501 raohan manish4501
and how can i get last name in the sequence.. see the last column for example 4500 so last name is updated as raju_4500 and coming to first name userId(1) and lastname is Bhatt_1 以及如何获取序列中的姓氏。.例如参见4500的最后一列,因此姓氏将更新为raju_4500并以名字userId(1)开头,姓氏为Bhatt_1
how could i get in sequence 我怎么能顺序
UserID FirstName LastName
1 AALIYAH Bhatt_0001
2 Mohan Kumar_0002
3 varun ratna_0003
4 suresh rania_0004
5 AARON suresh_0005
etc ......
4500 Kalyan raju_4500
4501 raohan manish4501
Suggest me 建议我
This expression gives you the length of the maximum UserID: 此表达式为您提供最大UserID的长度:
select len(max(userid)) from User
And you can put leading zeros on a number like this: 您可以在这样的数字上加上前导零:
select right('0000000000' + convert(varchar(10), /*number*/), /*length*/)
So, putting that together, I would do this: 因此,将它们放在一起,我会这样做:
declare @length int = (select len(max(userid)) from User)
update User set LastName = 'LastName_' +
right('0000000000' + convert(varchar(10), UserID), @length)
我认为这会起作用
update User set LastName = +'LastName_'+ RIGHT('0000'+ CONVERT(varchar(10),UserID),4)
This will give the exact result. 这将给出确切的结果。
update User set LastName = LastName +'_'+RIGHT('000'+cast(UserID as varchar), 4)
试试这个
update User set LastName = +'LastName_'+ RIGHT('0000'+ CONVERT(varchar(10),UserID),4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.