更新SQL Server中的列数据 [英] updating column data in sql server

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

问题描述

我有一个名为 Country 的表,如下所示

I have a table named as Country as follows

admin1code
    1
    10
    11
    12
    5
    6
    45



我想更新列数据,以便在数字的开头应添加0,它们是单个单位EX. 1,2,3 ... 9等变为01,02,03..09

因此更新后的最终列数据应类似于



I wants to update column data such that 0 should be appended at the beginning of number which are single unit EX. 1,2,3...9 etc becomes 01,02,03..09

so final column data after update should look like

admin1code
    01
    10
    11
    12
    05
    06
    45

推荐答案

,假设admin1code的数据类型为(n)varchar
assuming admin1code is of datatype (n)varchar
Update Country
set admin1code = right('00'+ admin1code, 2)


尝试:
UPDATE Country SET admin1code = '0' + admin1code WHERE LEN(admin1code) = 1 AND admin1code >= '0' and admin1code <='9'


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

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