本文介紹了如何修剪英國郵政編碼的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我只想顯示英國郵政編碼的首字母.例如,如果它的 WV14 7AR
那么我只想顯示 WV
I would like to only show the first letters of a UK postcode. For example if its WV14 7AR
then i only want to show WV
我試過了:
DECLARE @v VARCHAR(30)
SET @v='WV14 7AR'
SELECT LEFT(@v,LEN(@v)-6)
返回:WV
但是如果郵政編碼是:WV4
那么它會顯示:W
這是錯誤的.
However if the postcode is: WV4
then it will show: W
and this is wrong.
請記住,您可以獲得一個英國郵政編碼,它可以是 1 個字母!伯明翰郵政編碼為:B1 2AR,因此 LEFT 建議將失敗.
Bare in mind you can get a UK postcode which could be 1 letter! A Birmingham postcode is: B1 2AR and therefore LEFT suggestion will fail.
推薦答案
這將顯示僅顯示英國郵政編碼的首字母:
With Src (Postcode) As (
Select 'B1 1AB' Union All
Select 'W1A 1AB' Union All
Select 'WV14 1AB' Union All
Select 'GIR 1AB'
)
Select
Postcode,
RTRIM(LEFT(Postcode, PATINDEX('%[0-9]%', Postcode) - 1)) As FirstLetters
From Src
<小時>
Postcode FirstLetters
-------- ------------
B1 1AB B
W1A 1AB W
WV14 1AB WV
GIR 1AB GIR
<小時>
Upd:可以使用
Where Postcode Like '%[0-9]%'
甚至更嚴格
Where Postcode Like '[a-z]%[0-9]%[a-z]'
或處理
CASE WHEN Postcode Like '%[0-9]%'
THEN RTRIM(LEFT(Postcode, PATINDEX('%[0-9]%', Postcode) - 1))
ELSE Postcode
END As FirstLetters
這篇關于如何修剪英國郵政編碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!