我有一列数据是这样的:
PID
926185047
1003101114
1006090344
1019104206
1006081343
925085128
925091019
925094250
925113421我想把那些9为数字的前面都加上0变成这样PID
0926185047
1003101114
1006090344
1019104206
1006081343
0925085128
0925091019
0925094250
0925113421请教怎么弄好些? 谢谢啦
PID
926185047
1003101114
1006090344
1019104206
1006081343
925085128
925091019
925094250
925113421我想把那些9为数字的前面都加上0变成这样PID
0926185047
1003101114
1006090344
1019104206
1006081343
0925085128
0925091019
0925094250
0925113421请教怎么弄好些? 谢谢啦
--> Author :
--> Date : 2009-11-25 09:21:05
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (PID int)
insert into [tb]
select 926185047 union all
select 1003101114 union all
select 1006090344 union all
select 1019104206 union all
select 1006081343 union all
select 925085128 union all
select 925091019 union all
select 925094250 union all
select 925113421
SELECT RIGHT(10000000+PID,9)PID FROM TB
/*
PID
------------
936185047
013101114
016090344
029104206
016081343
935085128
935091019
935094250
935113421(9 個資料列受到影響)*/
set pid=right('0000000000'+pid,10)
where len(pid)<10
SELECT RIGHT(10000000+PID,10)PID FROM TB
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-25 09:20:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([PID] varchar(20))
insert [tb]
select 926185047 union all
select 1003101114 union all
select 1006090344 union all
select 1019104206 union all
select 1006081343 union all
select 925085128 union all
select 925091019 union all
select 925094250 union all
select 925113421
--------------开始查询--------------------------
update tb set pid=right('0000000000'+pid,10) where len(pid)<10
select * from tb
----------------结果----------------------------
/* PID
--------------------
0926185047
1003101114
1006090344
1019104206
1006081343
0925085128
0925091019
0925094250
0925113421(9 行受影响)
*/
set PID = right('0000000000' + PID, 9)
-- =============================================
-- Author: T.O.P
-- Create date: 2009/11/25
-- Version: SQL SERVER 2005
-- =============================================
declare @TB table([PID] int)
insert @TB
select 926185047 union all
select 1003101114 union all
select 1006090344 union all
select 1019104206 union all
select 1006081343 union all
select 925085128 union all
select 925091019 union all
select 925094250 union all
select 925113421select RIGHT('00'+CAST([PID] AS NVARCHAR(20)),10) AS [PID]
from @TB
--测试结果:
/*
PID
----------
0926185047
1003101114
1006090344
1019104206
1006081343
0925085128
0925091019
0925094250
0925113421(9 row(s) affected)
*/
SELECT right(('0'+right(PID,10)),10)--其中最里面的right(PID,10)是将PID转为字串格式
--而后'0'+right(PID,10)就可以在各个数字前面都加上了‘0’
--但因为要求只是9位数字前加‘0’,即保证结果位数是10位,所以再使用外层right(,10)取后10位就可以了
update table set pid='0'+pid where charindex('9',pid)=1