問(wèn)題描述
是否可以?xún)H使用 TSQL 來(lái)檢查 varchar 字段的前兩個(gè)字符是否按字母順序排列?
Is it possible, just using TSQL, to check if the first two characters of a varchar field are alphabetical?
我需要從 my_table
中只選擇 my_field
以?xún)蓚€(gè)字母字符開(kāi)頭的行.我怎樣才能做到這一點(diǎn)?
I need to select from my_table
only the rows having my_field
beginning with two alphabetical chars. How can I achieve this?
是否可以使用正則表達(dá)式?
Is it possible to use a regex?
推薦答案
你不需要使用正則表達(dá)式,LIKE
就足夠了:
You don't need to use regex, LIKE
is sufficient:
WHERE my_field LIKE '[a-zA-Z][a-zA-Z]%'
假設(shè)字母順序"僅指拉丁字符,而不是 Unicode 中按字母順序分類(lèi)的任何字符.
Assuming that by "alphabetical" you mean only latin characters, not anything classified as alphabetical in Unicode.
注意 - 如果您的排序規(guī)則區(qū)分大小寫(xiě),則將范圍指定為 [a-zA-Z]
很重要.[a-z]
可以排除 A
或 Z
.[A-Z]
可以排除 a
或 z
.
Note - if your collation is case sensitive, it's important to specify the range as [a-zA-Z]
. [a-z]
may exclude A
or Z
. [A-Z]
may exclude a
or z
.
這篇關(guān)于檢查字符串的起始字符是否在 T-SQL 中按字母順序排列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!