2014-01-30 33 views
2

我有以下在SAS中使用的代碼提取,並希望將其寫入SQL Server中以提取數據。如何在SQL Server中使用substr?

substr(zipname,1,4) in("2000","9000","3000","1000");run; 

如何在SQL Server中編寫此代碼?

我想和得到這個錯誤:

An expression of non-boolean type specified in a context where a condition is expected

+0

翻譯這意味着什麼? 「IN」條款應該怎麼做? –

+1

修復雙引號後,可以使用'left(zipname,4)'而不是'substring()'。 –

回答

5

在SQL Server中,有沒有substr功能(這是substring

順便說一下,你需要一個完整的查詢......

select blabla 
from blibli 
where substring(zipname, 1, 4) in ('2000', '9000', 3000', '1000') 

假設zipname是一個varchar或類似的東西...

+0

從現在起,我絕對會用'blabla'列命名我的臨時表'blibli'。 –

+1

@RaduGheorghiu不是最佳做法嗎? –

+0

謝謝。有效!爲什麼我沒有想到子字符串?謝謝。 – user3197575

1

您需要一張表來獲取記錄,並且zipname是表格中的一列。聲明會是這樣的:

select * from tablename where substring(zipname,1,4) in ('2000','9000','3000','1000') 
1

既然你想要第一個x個字符,你也可以使用left()函數。

where left(zipname, 4) in (values go here) 

請記住,您的值必須單引號。你的問題有雙引號。

+0

是不是'左(zipname,4)'? – Joe