2015-06-04 124 views
1

所以我是新的整個SQL查詢業務,但我需要一些幫助兩個問題。我的目標是在查詢結果中顯示列「NodeName」中具有「Database」一詞的「EnvironmentName」列中的任何內容。我這樣做是與查詢的SQL查詢刪除部分路徑/空

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 

預先感謝您!

+0

添加你嘗試1和2 – Sherlock

+0

你已經試過'查詢IS NOT NULL「,行仍然顯示?你確定這些字段中包含「NULL」而不僅僅是「NULL」字嗎?嘗試'ISNULL(EnvironmentName,'NULL')!='NULL'' – jradich1234

+0

這並不是說你的語法是無效的,因爲「不爲空」,它是這樣說的,因爲你有一個where子句,後跟一個order by,由另一個where子句。你只能有一個where子句,並且它必須在任何order之前。您可以使用和/或運算符連接條件。 – Xedni

回答

1

您的查詢是非常接近.. 1方式如下:您必須指定一個特定的列,而使用不爲空IS NOT 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/

+0

真棒工作@iplatz – TheAce

1

Where子句將語句 來,然後再爲了像

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

謝謝@Kaushik Thanki,但是現在當我嘗試運行該程序時出現錯誤:請參閱問題獲取新代碼/結果 – TheAce

+0

USE like this way [Backbone_ASPIDER]。[dbo]。[vw_CFGsvr_Con]。[ModuleName] –

+0

I我確信這有助於它現在說不妥協「。」 – TheAce

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