删除T-SQL中的最后一位数字

时间:2014-11-18 07:05:13

标签: sql sql-server tsql

如何从tsql中删除数据和组中的最后一个数字,
查询

select browser from Table1 group by browser

结果

Chrome11
Chrome28
Chrome30
Chrome31
Chrome32
Chrome33
Firefox12
Firefox22
Firefox30
Firefox32
Firefox33
IE10
IE7
IE8
IE9
IEMobile
IEMobile10
InternetExplorer11
Mozilla
Opera9
Safari
Safari5
Safari6

所需格式

  Chrome
  Firefox
  IE
  Safari
  InternetExplorer
  Mozilla
  IEMobile

注意:我们不会使用

的客户端浏览器

6 个答案:

答案 0 :(得分:1)

这将为你工作..

select distinct browser from (
select REPLACE (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE 
(browser, '0', ''),'1', ''),'2', ''),'3', ''),'4', ''),'5', ''),
          '6', ''),'7', ''),'8', ''),'9', '') as  browser
 from  TestTable  group by  browser)  temp

答案 1 :(得分:0)

您可以使用嵌套的REPLACE(),它最多可达32级:

DECLARE @mystring NVARCHAR(30)

SET @mystring = 'TEST76'

select REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@mystring, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')

结果:TEST

希望这有帮助。

答案 2 :(得分:0)

WITH ExampleCte(Browser) AS (
    SELECT 
        'Chrome11'           UNION ALL SELECT 
        'Chrome28'           UNION ALL SELECT 
        'Chrome30'           UNION ALL SELECT 
        'Chrome31'           UNION ALL SELECT 
        'Chrome32'           UNION ALL SELECT 
        'Chrome33'           UNION ALL SELECT 
        'Firefox12'          UNION ALL SELECT 
        'Firefox22'          UNION ALL SELECT 
        'Firefox30'          UNION ALL SELECT 
        'Firefox32'          UNION ALL SELECT 
        'Firefox33'          UNION ALL SELECT 
        'IE10'               UNION ALL SELECT 
        'IE7'                UNION ALL SELECT 
        'IE8'                UNION ALL SELECT 
        'IE9'                UNION ALL SELECT 
        'IEMobile'           UNION ALL SELECT 
        'IEMobile10'         UNION ALL SELECT 
        'InternetExplorer11' UNION ALL SELECT 
        'Mozilla'            UNION ALL SELECT 
        'Opera9'             UNION ALL SELECT 
        'Safari'             UNION ALL SELECT 
        'Safari5'            UNION ALL SELECT 
        'Safari6'
)
SELECT 
    Browser,
    Left(Browser, Len(Browser) - PatIndex('%[^0-9]%', Reverse(Browser)) + 1) AS BrowserWithoutNumber
FROM 
    ExampleCte

答案 3 :(得分:0)

你可以这样做:

测试数据:

DECLARE @tbl TABLE(browser VARCHAR(100))

INSERT INTO @tbl
VALUES
('Chrome11'),('Chrome28'),('Chrome30'),
('Chrome31'),('Chrome32'),('Chrome33'),
('Firefox12'),('Firefox22'),('Firefox30'),
('Firefox32'),('Firefox33'),('IE10'),
('IE7'),('IE8'),('IE9'),('IEMobile'),
('IEMobile10'),('InternetExplorer11'),('Mozilla'),
('Opera9'),('Safari'),('Safari5'),('Safari6')

然后是这样的查询:

;WITH CTE
AS
(
    SELECT
        (
        CASE 
            WHEN PATINDEX('%[0-9.-]%', browser) = 0 
            THEN browser
            ELSE SUBSTRING(browser,0,PATINDEX('%[0-9.-]%', browser)) 
        END
        ) AS browser
    FROM 
        @tbl AS tbl
)
SELECT
    *
FROM
    CTE
GROUP BY
    CTE.browser

这会得到这样的输出:

Chrome
Firefox
IE
IEMobile
InternetExplorer
Mozilla
Opera
Safari

参考:

答案 4 :(得分:0)

创建一个功能:

Create Function fnStrFromAlphaNum(@str varchar(100))
returns varchar(100)
as
begin
    if patindex('%[0-9]%',@str)>0
        set @str = SUBSTRING(@str,1,patindex('%[0-9]%',@str)-1)
    return @str
end

在您的查询中调用此函数:

select distinct fnStrFromAlphaNum(browser)
from table1

答案 5 :(得分:0)

你可以这样做:

WITH cte AS
   (SELECT rtrim(
              substring(
                 browser,
                 1,
                 PATINDEX('%[0-9]%', browser) -1
              )
           ) as browser
    FROM table)
SELECT cte.browser FROM cte
GROUP BY cte.browser