Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string contains leading letters

How can I check if my string contains leading letters? In C# is easy, but I am doing this in SQL. Is there a way to check this? If so, how can I remove it?

EX: @MyString = 'A1234'

Updated string = '1234'

like image 837
Musikero31 Avatar asked Nov 12 '10 02:11

Musikero31


People also ask

How do you check if a string contains a letter in SQL?

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

How do you make sure a string only has letters?

In order to check if a String has only Unicode letters in Java, we use the isDigit() and charAt() methods with decision-making statements. The isLetter(int codePoint) method determines whether the specific character (Unicode codePoint) is a letter. It returns a boolean value, either true or false.

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

The idea is to use the regular expression ^[a-zA-Z0-9]*$ , which checks the string for alphanumeric characters. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.


2 Answers

Use:

UPDATE YOUR_TABLE
   SET your_column = SUBSTRING(your_column, 2, DATALENGTH(your_column))
 WHERE your_column LIKE '[A-Za-z]%'
like image 172
OMG Ponies Avatar answered Oct 05 '22 21:10

OMG Ponies


For one leading letter, you can do:

IF NOT ISNUMERIC(SUBSTRING(@MyString, 1, 1))
    SET @MyString = SUBSTRING(@MyString, 2, LEN(@MyString)) 

You can repeat that until there are no more letters.

like image 36
BeemerGuy Avatar answered Oct 05 '22 19:10

BeemerGuy