473,537 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to trim leading zeroes when converting decimal() to char()

I need to convert a decimal field to char() but also trim the leading
zeroes. Any idea? I could not find any function to do that.
Nov 12 '05 #1
1 17321
I'm sure there's a better way, but:

with sample table testdec:

create table testdec (col1 dec(10,5))

some sample data:

insert into testdec values (0005.500),(50.5),(12345.12345)

We then take a substring on a char of the first 5 characters (the
precision of the decimal value), convert it into an int to drop the
leading zeros, convert it back into a char, and then perform a right
trim (rtrim) on it to get rid of the padded blanks. We concatenate this
with the substring of the decimal separator and the digits to the right
of the decimal point (the scale), and convert this into a char.

select rtrim
( char(int(substr(char(col1),1,5))) )
concat
substr(char(col1),6,6) from testdec

1
-----------------
5.50000
50.50000
12345.12345

Like any example I provide, please test extensively with boundary
conditions.

Jean-michel wrote:
I need to convert a decimal field to char() but also trim the leading
zeroes. Any idea? I could not find any function to do that.


Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13758
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10 digits), the first 4 being the month and year (mmyy)
22
2097
by: pemo | last post by:
Can anyone suggest a nice way to trim any leading zeros off of a string, e.g., I'd like to take a string like 00000012 and convert it to 12. Ta pemo
3
2095
by: Bob Stearns | last post by:
I wish to return a dosage (DECIMAL(7,3)) concatenated with its units (VARCHAR(15); i.e., 1.500 cc. When I do this as shown below, I get 001.500 cc. Is there any easy way to elide the leading zeroes? CAST(t4.dosage AS CHAR)||' '||t5.dosage_units AS dose, I get the same result with the CHAR function and, of course, concatenation does not...
1
1590
by: Mike P | last post by:
I'm trying to write the contents of a csv file to a table, but I am having problems with fields with leading zeroes. Whenever I save as csv I lose the leading zeroes. Does anybody know how to prevent this? *** Sent via Developersdex http://www.developersdex.com ***
31
2869
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char *trim(char *s) { char *begin,*end; begin = s;
8
46925
by: stainless | last post by:
Is it possible, using the ToString function, to take an integer and conver to a string of fiexd width with the leading spaces padded with zeroes. e.g. integer 123 converted to a string of length 9 with 6 leading zeroes i.e. 000000123 integer 98765432 converted to a string of length 9 with 1 leading zero i.e. 098765432
9
3009
by: jbaranski | last post by:
Access 2003 on XP pro machine... i'm running a crosstab query and an export to a 3rd party company showing different benefit plans for employees; specifically dental, vision and medical pulling the effective dates (date which the plan when into effect). generally the plans all have the same effective date. however in the case they don't 2 lines...
121
4997
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
2
2818
by: DanCole42 | last post by:
Newbie question, here. I have a database that frequently imports ZIP code data that frequently needs cleaning: 12365 6487 64684-3543 3213-6546
0
7365
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7301
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7281
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3353
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3345
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1761
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.