在条件下使用替换功能

时间:2014-03-03 10:26:37

标签: sql sql-server-2008 sql-server-2005 replace

我是SQL新手。我有一个SQL查询

select replace('P' + ltrim(rtrim(upper(columnvalue))),'PP','P') from tablename

这里我将列值设为P0987799, R0987884, 9745456 OR RT087477(例如)

现在我想从该查询中获取单一格式的值(从p开始,然后是数字)。好像该值以p (P0987799)开头,那么没问题。

但价值从RRT开始,然后应替换为P

怎么做?

2 个答案:

答案 0 :(得分:2)

这是一个想法......

DECLARE @your_table table (
   columnvalue varchar(50)
);

INSERT INTO @your_table (columnvalue)
  VALUES ('P0987799')
       , ('R0987884')
       , ('9745456')
       , ('RT087477')
;

-- Verbose version (shows individual steps)
SELECT columnvalue
     , start_of_numbers
     , numeric_part
     , 'P' + numeric_part As prefixed_with_p
FROM   (
        SELECT columnvalue
             , start_of_numbers
             , SubString(columnvalue, start_of_numbers, 50) As numeric_part --50 = length of field (varchar(50))
        FROM   (
                SELECT columnvalue
                     , PatIndex('%[0-9]%', columnvalue) As start_of_numbers -- find the first number
                FROM   @your_table
               ) As x
       ) As y
;

-- Shortened version
SELECT columnvalue
     , 'P' + SubString(columnvalue, PatIndex('%[0-9]%', columnvalue), 50) As prefixed_with_p
FROM   @your_table
;

首先查找第一个数字字符的位置,然后在该点之前剥离任何内容(只留下数字)。然后在开头贴一个“P”!

结果:

columnvalue  start_of_numbers  numeric_part  prefixed_with_p
------------ ----------------- ------------- ----------------
P0987799     2                 0987799       P0987799
R0987884     2                 0987884       P0987884
9745456      1                 9745456       P9745456
RT087477     3                 087477        P087477


columnvalue  prefixed_with_p
------------ ----------------
P0987799     P0987799
R0987884     P0987884
9745456      P9745456
RT087477     P087477

编辑:如果您想缩小前缀范围:

SELECT columnvalue
     , start_of_numbers
     , numeric_part
     , alpha_part
     , CASE WHEN alpha_part IN ('P', 'R', 'RT') THEN -- limit the prefixing
         'P' + numeric_part
       ELSE
         numeric_part
       END As prefixed_with_p
FROM   (
        SELECT columnvalue
             , start_of_numbers
             , SubString(columnvalue, start_of_numbers, 50) As numeric_part --50 = length of field (varchar(50))
             , SubString(columnvalue, 0, start_of_numbers) As alpha_part
        FROM   (
                SELECT columnvalue
                     , PatIndex('%[0-9]%', columnvalue) As start_of_numbers
                FROM   @your_table
               ) As x
       ) As y
;

结果:

columnvalue  start_of_numbers  numeric_part  alpha_part  prefixed_with_p
------------ ----------------- ------------- ----------- ----------------
P0987799     2                 0987799       P           P0987799
R0987884     2                 0987884       R           P0987884
9745456      1                 9745456                   9745456
RT087477     3                 087477        RT          P087477

答案 1 :(得分:1)

请查看以下查询

 DECLARE @your_table table (
   columnvalue varchar(50)
);

INSERT INTO @your_table (columnvalue)
  VALUES ('P0987799')
       , ('R0987884')
       , ('9745456')
       , ('RT087477')



select CASE WHEN (ColumnValue like 'R%' OR ColumnValue like 'P%'  ) THEN 'P' eLSE '' END +
SUBSTRING(ColumnValue,PATINDEX('%[0-9]%',ColumnValue),LEN(ColumnValue)-(PATINDEX('%[0-9]%',ColumnValue))+1) as FinalValue
from @your_table