我需要撰寫一個 T-SQL 程序,其中引數是郵政編碼。我們已經宣告了以下引數。
declare @postal_code varchar(10)
表中的示例資料:
| 郵政編碼 |
|---|
| 空值 |
| 46383 |
| 074523632 |
| B4H34 |
| 113601419 |
| ZH/8600 |
| A1G 9Z9 |
| WN73R |
| Wd3 3he |
| 89136 |
等等。我們有各種各樣的地址,有些沒有某些外國的郵政編碼,有些則有標準的 5 位或 10 位美國郵政編碼。
我需要以某種方式對查詢進行編碼以說:
select *
from table_name
where postal_code = @postal_code
我最初的代碼是這樣的:
select *
from table_name
where (@postal_code is null or
left(ad.postal_code, 5) = @postal_code)
但這不適用于除 5 位數字以外的任何其他數字,然后我嘗試使用 10 位數字,但 5 位數字不匹配。我需要去除空格或其他字符嗎?我嘗試搜索并且有多種解決方案,但我需要一些適用于國內外各種郵政編碼的解決方案。
uj5u.com熱心網友回復:
根據您的最后一條評論,您似乎需要一個“以”開頭的查詢。嘗試這個
SELECT *
FROM table_name
WHERE REPLACE(postal_code, ' ', '') LIKE REPLACE(@postal_code, ' ', '') '%';
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311532.html
標籤:sql sql-server 查询语句 邮政编码 邮政编码
