1

我需要从中提取信息的数据库中有许多字段。对于名称列,数据采用以下格式:

Jim John - 1234567

我只想要名称部分,因此从“-”开始修剪所有内容。我试过以下

"SELECT REPLACE(LEFT(name, CHARINDEX('-',name)-1),'_',' '), othername, streetname, postal, 
                                        nameID, helper, helperID

        FROM [DATABASE].[dbo].[nameDB] with (nolock) 

        WHERE
              (othername like '" & txtSearch.Text & "' 
               or postal like '" & txtSearch.Text & "') 
               and (postal not like '%Error%' 
               and postal not like '%nas_admin%'
               and postal not like '%NASAdmin%'
               and postal like '[A-z][A-z][A-z]%')"

这不起作用,我是否可以修剪数据并在其他字段中搜索其他数据?

谢谢你的帮助

4

2 回答 2

1

试试这个Substring

  SELECT 
    SUBSTRING(name, 0, CHARINDEX('-', name)) AS [name] , othername, streetname, postal, 
                                        nameID, helper, helperID

        FROM [ARDVARC].[dbo].[nameDB] with (nolock) 

        WHERE
              (othername like '" & txtSearch.Text & "' 
               or postal like '" & txtSearch.Text & "') 
               and (postal not like '%Error%' 
               and postal not like '%nas_admin%'
               and postal not like '%NASAdmin%'
               and postal like '[A-z][A-z][A-z]%')"

希望这可以帮助。

于 2015-12-04T08:20:46.093 回答
0
  SELECT 
        PARSENAME(REPLACE('Jim John - 1234567','-','.'),2)

Result:

name      
--------- 
Jim John  

So you can use it as

SELECT 
     PARSENAME(REPLACE(NAME, '-', '.'), 2) NAME
    ,PARSENAME(REPLACE(othername, '-', '.'), 2) othername
    ,streetname
    ,postal
    ,nameID
    ,helper
    ,helperID
FROM [DATABASE].[dbo].[nameDB] WITH (NOLOCK)
WHERE ...

Parsename(),Replace()

于 2015-12-05T11:59:26.720 回答