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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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...
lcohanDatabase AnalystCommented:
select case when isnumeric('000123456') = 1 then cast('000123456' as int) else 'that is a non-numevic value' end;
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Jim HornMicrosoft SQL Server Data DudeCommented:
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
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?
Jim HornMicrosoft SQL Server Data DudeCommented:
=1 means true the value is numeric, and =0 means false not numeric.  Very handy.
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!
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.