15

最初の 2 文字がゼロの場合、どのように置き換えますか?

例:

 1. 00001
 2. 00123
 3. 02451

次のようにする必要があります。

 1. 11001
 2. 11123
 3. 02451

編集:言及するのを忘れていましたが、select句(ビュー内)でこれが必要ですありがとうございます。

4

4 に答える 4

34
update  YourTable
set     col1 = '11' + substring(col1, 3, len(col1)-2)
where   col1 like '00%'

ビューでは、次のようにできます。

select   case
         when col1 like '00%' then stuff(col1, 1, 2, '11')
         else col1
         end
from     YourTable;

SQL Fiddle での実例。

于 2013-01-08T14:16:39.597 に答える
9
declare @a varchar(10)

select @a='01123'

Select case when LEFT(@a,2)='00' then STUFF(@a,1,2,'11') else @a end
于 2013-01-08T14:18:27.690 に答える
4

以下のような左の方​​法を使用することもできます

select case When left(Name,2) = '00' Then stuff(Name, 1, 2, '11')
     else Name
     end
 from YourTable
于 2013-01-08T14:31:31.807 に答える