SQL查询删除部分路径/空

时间:2015-06-04 17:34:45

标签: sql sql-server

所以我是整个SQL Query业务的新手,但我需要一些有关两个问题的帮助。我的目标是在“EnvironmentName”列中包含任何在“NodeName”列中包含“Database”一词的内容,以显示在查询结果中。我这样做了

  FROM [Backbone_ASPIDER].[dbo].[vw_CFGsvr_Con]
  WHERE NodeName = 'Database' 
  ORDER BY EnvironmentName asc
  WHERE NodePath  

查询结果:

我可以获取查询结果,但希望删除 NULL 的行。我曾尝试使用“IS NOT NULL”,但SQL Server Management Studio将其称为“错误的语法。”

我尝试过:

FROM [Backbone_ASPIDER].[dbo].[vw_CFGsvr_Con]
WHERE NodeName = 'Database' 
ORDER BY EnvironmentName asc IS NOT NULL
WHERE NodePath  

提前谢谢!

3 个答案:

答案 0 :(得分:1)

Where子句将首先出现然后按语句排序 喜欢以下方式

url[-4:-1]

答案 1 :(得分:1)

您的查询非常接近.. 1:使用IS NOT NULL时,必须指定特定列不为null。 因此,请将您的查询修改为:

FROM [Backbone_ASPIDER].[dbo].[vw_CFGsvr_Con]
WHERE NodeName = 'Database' AND EnvironmentName IS NOT NULL 
ORDER BY EnvironmentName asc
WHERE NodePath

2:查看本文关于从查询结果中修剪字符串的部分内容 http://basitaalishan.com/2014/02/23/removing-part-of-string-before-and-after-specific-character-using-transact-sql-string-functions/

答案 2 :(得分:1)

编辑:我刚刚注意到你从你的OP中删除了这个,所以如果你照顾它,请随意忽略。

我认为没有人解决子字符串问题。你有几种方法可以达到这个目标,这取决于你需要切割的字符串有多复杂,但这就是我要做的事情

-- Populating some fake data, representative of what you've got
if object_id('tempdb.dbo.#t') is not null drop table #t
create table #t
(
    nPath varchar(1000)
)

insert into #t
select '/Database/Mappings/Silver/Birthday' union all
select '/Database/Connections/Blue/Happy'


-- First, get the character index of the first '/' after as many characters the word '/database/' takes up.
-- You could have hard coded this value too. Add 1 to it so that it moves PAST the slash.
;with a as
(
    select 
        ixs = charindex('/', nPath, len('/Database/') + 1),
        -- Get everything to the right of what you just determined with all the charindex() stuff
        ss = right(nPath, len(nPath) -  charindex('/', nPath, len('/Database/') + 1)),
        nPath
    from #t
)
-- Now just take the left of the now-cleaned-up string from start to the first pipe
select
    ixs,
    ss,
    color = left(ss, charindex('/', ss) -1),
    nPath
from a