Hello,
How to check if the first character of a string is a number.
Thanks for your help.
Hello,
How to check if the first character of a string is a number.
Thanks for your help.
SELECT c
FROM (VALUES
('abcb')
,('1bcd')) t(c)
WHERE
ISNUMERIC(LEFT(c,1))=1;
Alternatively,
SELECT c
FROM (VALUES
('abcb')
,('1bcd')) t(c)
WHERE
c LIKE '[0-9]%'
Careful now... you should NEVER mistake ISNUMERIC() as meaning "ISALLDIGITS" because that's not what it's for. Try the following as an example.
SELECT c
FROM (VALUES
('abcb')
,('1bcd')
,(',bcd')
,('$bcd')
,('\bcd')
) t(c)
WHERE
ISNUMERIC(LEFT(c,1))=1
;
Given
it might be that following characters are, also, important ... so the test might need to be
c LIKE '[0-9]%'
OR c LIKE '[-+][0-9]%'
There again I might be overthinking the problem ...
Good point @JeffModen. I wasn't event thinking about the ISNUMERIC behavorio when I suggested that OP use the LIKE '[0-9]%' construct. I was only thinking about avoiding the function calls and ability to use indexes if any.