Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2008, 09:02 PM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default getting the first non-zero number in a field

Hi,

  Anyone who can help me to get the first non-zero number of a field? I have a column in my table (varchar) which contains a series of numbers. I need to get the first non-zero number in the column. Charindex and patindex can get the position of the zero, but I can't get the positition of non-zero using these functions. I hope you have a better idea to do this.

Thanks a lot in advance!

 
Old March 26th, 2008, 09:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.

Just thinking out loud, what if you convert the varchar to number, convert it again to string, count the quantity of characters there, and substract the quantity of the original field from this number?

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old March 26th, 2008, 10:32 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I think something like this might work. (Its late so it may not be accurate but it furthers Gonzalos thought)

SELECT
(LEN(<varcharfield>) - (Len(Convert(int, <varcharfield>))))
From <table>

If the varchar length is 10 and is padded with 2 zeros the select statment above would return the result: 2

varchar Field length == 10
int field length == 8
difference == 2

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 27th, 2008, 03:57 AM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much guys! It works!







Similar Threads
Thread Thread Starter Forum Replies Last Post
convert time field to number jobalistic Access 17 February 22nd, 2013 12:40 PM
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
Populate Auto-Number Field JeffGirard Access 2 August 26th, 2005 06:49 AM
Sequential numbering a non auto number field dbartelt Access 3 May 5th, 2005 02:56 PM
Assigning string to a number field p_gauri7 Crystal Reports 3 December 10th, 2004 10:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.