如何用oracle中的另一个值替换列的前两个字符

时间:2013-04-11 05:01:17

标签: sql oracle11g

我有一个表格如下。

TAX_ID_ID CUST_ID TAX_ID_TYP_CD TAX_ID_NO TAX_ID_DRV_UPCS_NO
1005004 2005004 ssn 100000006 100000006
1005006 2005006 ssn 100000007 100000007
1005009 2005009 ssn 100000008 100000008
1005012 2005012 ssn 100000009 100000009
1005014 2005014 ssn 100000010 100000010
1005017 2005017 ssn 100000011 100000011

我想将TAX_ID_NO,TAX_ID_DRV_UPCS_NO的前两个字符替换为25而不是10。列数据类型为varchar2。什么是此

的更新查询

2 个答案:

答案 0 :(得分:1)

我认为这将满足您的需求。

<强> Replace

<强> Substring

<强> String Concat

replace(TAX_ID_NO,TAX_ID_NO,'25'||substr( TAX_ID_NO, 3,length(TAX_ID_NO)-2))
replace(TAX_ID_DRV_UPCS_NO ,TAX_ID_DRV_UPCS_NO ,'25'||substr( TAX_ID_DRV_UPCS_NO , 3,length(TAX_ID_NO)-2))

修改

最后你必须Update表格为

UPDATE TABLE
   SET TAX_ID_NO = replace(TAX_ID_NO,TAX_ID_NO,'25'||substr( TAX_ID_NO, 3,length(TAX_ID_NO)-2)),
   SET TAX_ID_DRV_UPCS_NO = replace(TAX_ID_DRV_UPCS_NO ,TAX_ID_DRV_UPCS_NO ,'25'||substr( TAX_ID_DRV_UPCS_NO , 3,length(TAX_ID_NO)-2))

答案 1 :(得分:0)

请尝试:

select 
  '25'||substr(TAX_ID_NO, 3), 
  '25'||substr(TAX_ID_DRV_UPCS_NO, 3) 
From YourTable;

使用

更新您的表格
update YourTable
set TAX_ID_NO='25'||substr(TAX_ID_NO, 2),
    TAX_ID_DRV_UPCS_NO='25'||substr(TAX_ID_DRV_UPCS_NO, 2);