我有个字段是email的想截取字段中@符号前面的部分如下[email protected]
[email protected]
[email protected]
-----------------------
结果
xxx
aa
1
怎么用SQL处理??

解决方案 »

  1.   

    select left(col,charindex('@',col)-1) from tb col 是字段
      

  2.   

    [code=SQL]--> 测试数据:#1
    if object_id('tempdb.dbo.#1') is not null drop table #1
    create table #1([TTT] varchar(13))
    insert #1
    select '[email protected]' union all
    select '[email protected]' union all
    select '[email protected]'select * ,SUBSTRING(TTT,1,CHARINDEX('@',TTT)-1)
    from #1
    [/code]
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-10-26 21:53:04
    -- 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.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(13))
    insert [tb]
    select '[email protected]' union all
    select '[email protected]' union all
    select '[email protected]'
    --------------开始查询--------------------------
    select left(col,patindex('%@%',col)-1) from tb
    ----------------结果----------------------------
    /* -------------
    xxx
    aa
    1(3 行受影响)*/