Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if starting characters of a string are alphabetical in T-SQL

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?

like image 345
davioooh Avatar asked Sep 13 '12 13:09

davioooh


People also ask

How check string is alphanumeric in SQL?

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.

How do you check if a column has alphabets in SQL?

To check if string contains letters uses the operator LIKE with the following regular expression '[A-Za-z]%'.


2 Answers

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.

like image 66
Damien_The_Unbeliever Avatar answered Oct 09 '22 11:10

Damien_The_Unbeliever


select * from my_table where my_field Like '[a-z][a-z]%'
like image 3
podiluska Avatar answered Oct 09 '22 09:10

podiluska