If a value is numeric, remove leading 0's

Hello - I have a field that stores an ID that can be numeric or an alpha code.

I'm trying to find a way to remove leading 0's for the field if it is a numeric code.

For example, the field values can be as follows:
0111
1234
0022
D123

I have an expression that worked in MS Access, but when I try to apply the code in SQL Server, it does not work.
Example of expression that works in MS Access; IIF(IsNumeric([ID]),INT([ID]),[ID]).

Any thoughts on how to recreate this functionality in SQL Server?
Cynthia HillLead ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
This should do the trick I think.

If ID is numeric, convert it to an INT to drop the leading zeroes then back to string, else accept the string as input:

select case when isnumeric([ID]) = 1 then cast(cast(ID as int) as varchar) else [ID] END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guru JiCommented:
SELECT
SUBSTRING(ID, PATINDEX('%[^0 ]%', ID+ ' '), LEN(ID))
FROM TABLENAME
WHERE ISNUMERIC([ID]) = 1
GO


Try this...
0
lcohanDatabase AnalystCommented:
select case when isnumeric('000123456') = 1 then cast('000123456' as int) else 'that is a non-numevic value' end;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Case when isnumeric(column) = 1
   THEN CAST(CAST(column as int) as varchar(25)
   THEN column END

Since both int and chat values exist in the same column then the column needs to be a chat data type, so for the numbers convert to int, which does not support leading zeros, then convert to chat
0
Cynthia HillLead ConsultantAuthor Commented:
Steve W. - That seems to do the trick. Thanks!!

Question...what exactly is the '=1' portion of the statement doing in "isnumeric([ID]) = 1"

Does 1 just represent  "TRUE"?...so conversationally...if isnumeric([ID]) is True, then turn the number into and int...otherwise just give me the ID?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
=1 means true the value is numeric, and =0 means false not numeric.  Very handy.
0
Steve WalesSenior Database AdministratorCommented:
>>Does 1 just represent  "TRUE"?...so conversationally...if isnumeric([ID]) is True, then turn the number into and int...otherwise just give me the ID?

Correct!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.