Skip to content
Advertisement

How do identify the first character of a string as numeric or character in SQL

I need to identify the first character in my data as numeric or character in SQL Server. I am relatively new to this and I don’t know where to begin on this one. But here is what I have done to this point. I had data that looked like this:

TypeDep
Transfer From 4Z2
Transfer From BZZ
Transfer From 123
Transfer From abc

I used the right function to remove the ‘transfer from’ and isolate the data I need to check.

UPDATE #decode
SET firstPartType = Right(z.TypeDep,17)
FROM #decode z
where z.TypeDep like 'TRANSFER FROM%'

firstPartType
4Z2
BZZ
123
abc

Now I need to add a column identifying the first character in the string. Producing the results below.

firstPartType   SecondPartType
4Z2             Numeric
BZZ             Alpha
123             Numeric
abc             Alpha

Advertisement

Answer

Using LEFT and ISNUMERIC(), however be aware that ISNUMERIC thinks some additional characters such as . are numeric

UPDATE #decode
  SET SecondPartType =  
      CASE WHEN ISNUMERIC(LEFT(firstPartType, 1)) = 1 THEN'Numeric'
      ELSE 'Alpha'
      END 
FROM #decode;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement