Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL TRIM Leading zeros.

Posted on 2012-04-04
8
Medium Priority
?
597 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 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 2000 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 70

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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