1101
1101

Reputation: 15

SQL - Add a letter prefix if the column starts with a number

I'm working on a project that is messing up when any results start with a number, for example 01.

I've tried to spend some time on how to apply a prefix to the column only if it starts with a number, though I'm stuck on the conditional part.

SELECT 'a' + CAST(ID AS VARCHAR(MAX)) FROM table

How could I apply the conditional to a select only if ID starts with a number?

Upvotes: 0

Views: 597

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269953

You would use case:

SELECT (CASE WHEN id LIKE '[0-9]%' THEN 'a' +  id
             ELSE id
        END) as newID
FROM table;

ID is presumably a string to start with, so you don't need to cast it to a varchar().

Upvotes: 2

Related Questions