Is it possible, just using TSQL, to check if the first two characters of a varchar field are alphabetical?
I need to select from my_table
only the rows having my_field
beginning with two alphabetical chars. How can I achieve this?
Is it possible to use a regex?
Answer: To test a string for alphanumeric characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. The string value that you are testing. This function will return a null value if string1 is alphanumeric.
To check if string contains letters uses the operator LIKE with the following regular expression '[A-Za-z]%'.
You don't need to use regex, LIKE
is sufficient:
WHERE my_field LIKE '[a-zA-Z][a-zA-Z]%'
Assuming that by "alphabetical" you mean only latin characters, not anything classified as alphabetical in Unicode.
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
.
select * from my_table where my_field Like '[a-z][a-z]%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With