Trim 符号后的所有内容并搜索其他字段 SQL
Trim everything after symbol and also search for other fields SQL
我需要从数据库中提取信息的多个字段。对于名称列,数据采用以下格式:
Jim John - 1234567
我只想要名称部分,因此 trim 从“-”开始的所有内容。我试过以下方法
"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]%')"
这行不通,我可以 trim 数据并在其他字段中搜索其他数据吗?
感谢您的帮助
尝试使用 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]%')"
希望这对您有所帮助。
SELECT
PARSENAME(REPLACE('Jim John - 1234567','-','.'),2)
结果:
name
---------
Jim John
因此您可以将其用作
SELECT
PARSENAME(REPLACE(NAME, '-', '.'), 2) NAME
,PARSENAME(REPLACE(othername, '-', '.'), 2) othername
,streetname
,postal
,nameID
,helper
,helperID
FROM [DATABASE].[dbo].[nameDB] WITH (NOLOCK)
WHERE ...
我需要从数据库中提取信息的多个字段。对于名称列,数据采用以下格式:
Jim John - 1234567
我只想要名称部分,因此 trim 从“-”开始的所有内容。我试过以下方法
"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]%')"
这行不通,我可以 trim 数据并在其他字段中搜索其他数据吗?
感谢您的帮助
尝试使用 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]%')"
希望这对您有所帮助。
SELECT
PARSENAME(REPLACE('Jim John - 1234567','-','.'),2)
结果:
name
---------
Jim John
因此您可以将其用作
SELECT
PARSENAME(REPLACE(NAME, '-', '.'), 2) NAME
,PARSENAME(REPLACE(othername, '-', '.'), 2) othername
,streetname
,postal
,nameID
,helper
,helperID
FROM [DATABASE].[dbo].[nameDB] WITH (NOLOCK)
WHERE ...