Solved

SQL TRIM Leading zeros.

Posted on 2012-04-04
8
576 Views
Last Modified: 2012-04-09
Hi Experts,

Ok in SQL how do I trim leading zeros?

So 000001 becomes 1

Thanks,
Dean
0
Comment
Question by:deanlee17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 37806395
you can convert it to a numeric type

cast('000001' as int)

if you need the result to remain a string, double cast it

cast(cast('000001' as int) as varchar)
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 37806397
SELECT (CAST Col1 AS int) AS Col1
0
 
LVL 3

Expert Comment

by:tsnirone
ID: 37806415
This is varchar ?

Ie. if its varchar 6:

select substring(ColName, patindex('%[^0]%',ColName), 6)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:deanlee17
ID: 37806446
Hmmm its actually a char field. Its part of a table join and its part of a substring, its currently...

SUBSTRING(dbo.AdmFormData.KeyField, 7, 10)
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 37806472
same idea except cast to char(10) instead of varchar

cast(cast(SUBSTRING(dbo.AdmFormData.KeyField, 7, 10) as int) as char(10))
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37806818
"best" - maybe, maybe not.

failure might be the best option.

  If the results are supposed to be numeric,  then trimming  '000xyz'  to 'xyz'  wouldn't be very helpful.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37807012
Trying to cast it to an INT, as you're doing, would fail and cause the entire statement/batch to fail.

The other way, the command doesn't fail, and every other row could still be processed correctly.

You've just pointed out the main reason the other way is much, much better.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37807052
>>> It would fail and give an error trying to cast it to an INT, as you're doing.

exactly - I want it to do that.
I wouldn't have done a numeric conversion if I didn't want to force numeric data (int, was just an example,  I'd suggest float or money or something else if the data looked different)

>>> The other way, the command doesn't fail

yes/no .  That exact conversion might succeed, but that doesn't mean it really "worked"

if I'm using the values as if they were numeric, in addition to simply formatting them then, as noted before  '0000xyz'  trimmed to 'xyz'  isn't an improvement.  It's simply hiding the error so it'll show up in later processing.  I'd much rather have my errors show up early.

If, on the other hand,  non-numeric data is ok, then of course, use the pattern searching, or some other non-numeric conversion.
however, there is nothing to indicate one way or the other (except for the author's acceptance)
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question