Solved

SQL TRIM Leading zeros.

Posted on 2012-04-04
8
570 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
8 Comments
 
LVL 73

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 73

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 73

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 73

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

785 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