2013-12-18 61 views
1

以下查詢正確運行,但未添加其他"OR"參數。過濾日期時的多個OR語句SQL Server 2008

由於某種原因,SC和CC的查詢結果未格式化爲WHERE子句,並將多個日期返回給我的參數之外。我應該如何更改此語法以確保我的WHERE子句影響OR語句?

SELECT DISTINCT case_number, case_filed_date 
FROM [case] 
WHERE case_filed_date BETWEEN '2012/07/01' AND '2012/09/30' 
AND case_number LIKE '%CA%' OR case_number LIKE '%SC%' OR case_number LIKE '%CC%' 

回答

1

試試這個使用括號爲AND具有更高的優先級高於OR: -

SELECT DISTINCT case_number, case_filed_date 
FROM [case] 
WHERE case_filed_date BETWEEN '2012/07/01' AND '2012/09/30' 
AND 
(case_number LIKE '%CA%' OR case_number LIKE '%SC%' OR case_number LIKE '%CC%') 
+0

嘿感謝!這解決了這個問題。 – user3116111

1

添加括號

SELECT DISTINCT case_number, case_filed_date 
FROM [case] 
WHERE case_filed_date BETWEEN '2012/07/01' AND '2012/09/30' 
AND 
(
    case_number LIKE '%CA%' OR case_number LIKE '%SC%' OR case_number LIKE '%CC%' 
) 

and結合強於or