473,889 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get substring of numeric value

I would like to extract the first 6 digits of a numeric value (e.g.
the string '123456' out of the numeric 123456789012345 6789). I tried
a combination of CAST and SUBSTR, but it seems I am not doing the
conversion properly. Please advice:

$ cat substr.sql
connect to viper
@

create table largenum (id integer not null, lval decimal(21,0) not
null)
@

insert into largenum values (1, 123456789012345 6789)
@

select lval from largenum
@

select substr(cast(lva l as char), 1, 6) from largenum
@

drop table largenum
@

connect reset
@

terminate
@

$ db2 -td@ -f substr.sql

Database Connection Information

Database server = DB2/LINUX 9.5.0
SQL authorization ID = DB2INST3
Local database alias = VIPER
DB20000I The SQL command completed successfully.

DB20000I The SQL command completed successfully.
LVAL
-----------------------
123456789012345 6789.

1 record(s) selected.
SQL0138N A numeric argument of a built-in string function is out of
range.
SQLSTATE=22011

DB20000I The SQL command completed successfully.

DB20000I The SQL command completed successfully.

DB20000I The TERMINATE command completed successfully.

--
Apr 9 '08 #1
4 24701
On Apr 9, 2:42 pm, "Serman D." <serma...@hotma il.comwrote:
I would like to extract the first 6 digits of a numeric value (e.g.
the string '123456' out of the numeric 123456789012345 6789). I tried
a combination of CAST and SUBSTR, but it seems I am not doing the
conversion properly. Please advice:

$ cat substr.sql
connect to viper
@

create table largenum (id integer not null, lval decimal(21,0) not
null)
@

insert into largenum values (1, 123456789012345 6789)
@

select lval from largenum
@

select substr(cast(lva l as char), 1, 6) from largenum
@

drop table largenum
@

connect reset
@

terminate
@

$ db2 -td@ -f substr.sql

Database Connection Information

Database server = DB2/LINUX 9.5.0
SQL authorization ID = DB2INST3
Local database alias = VIPER

DB20000I The SQL command completed successfully.

DB20000I The SQL command completed successfully.

LVAL
-----------------------
123456789012345 6789.

1 record(s) selected.

SQL0138N A numeric argument of a built-in string function is out of
range.
SQLSTATE=22011

DB20000I The SQL command completed successfully.

DB20000I The SQL command completed successfully.

DB20000I The TERMINATE command completed successfully.

--
You need to cast it to something bigger than a single char. I.e.:

db2 "select substr(cast(lva l as char(22)),3,6) from largenum"

1
------
123456

1 record(s) selected.

/Lennart

Apr 9 '08 #2
On Apr 9, 3:20 pm, Lennart <Erik.Lennart.J ons...@gmail.co mwrote:
db2 "select substr(cast(lva l as char(22)),3,6) from largenum"
Thank you for your replay Lennart. I have some follow-up questions:

1. Why are leading zeros added in the cast from numeric to char?
2. How do I reliably remove them?
3. Consider the below sample: What query returns the string '123456'
for the all rows?

create table largenum (lval decimal(21,0) not null)
@

insert into largenum values
(12345678901234 56789),
( 123456789012345 678),
( 123456789012345 67),
( 123456789012345 6),
( 123456789012345 ),
( 12345678901234) ,
( 1234567890123),
( 123456789012)
@

select substr(cast(lva l as char(64)), 3, 6) as as_char from largenum
@

AS_CHAR
-------
123456
012345
001234
000123
000012
000001
000000
000000

8 record(s) selected.

--
Apr 10 '08 #3
your previous code indicates you are on Version 9
Database server = DB2/LINUX 9.5.0
SQL authorization ID = DB2INST3
Local database alias = VIPER
Did you try the STRIP function already?

db2 "values(STRIP(' 00123',L,'0'))"

1
-----
123
Regards,
Florian
Apr 11 '08 #4
On Apr 10, 9:22 am, "Serman D." <serma...@hotma il.comwrote:
On Apr 9, 3:20 pm, Lennart <Erik.Lennart.J ons...@gmail.co mwrote:
db2 "select substr(cast(lva l as char(22)),3,6) from largenum"

Thank you for your replay Lennart. I have some follow-up questions:

1. Why are leading zeros added in the cast from numeric to char?
2. How do I reliably remove them?
3. Consider the below sample: What query returns the string '123456'
for the all rows?

create table largenum (lval decimal(21,0) not null)
@

insert into largenum values
(12345678901234 56789),
( 123456789012345 678),
( 123456789012345 67),
( 123456789012345 6),
( 123456789012345 ),
( 12345678901234) ,
( 1234567890123),
( 123456789012)
@

select substr(cast(lva l as char(64)), 3, 6) as as_char from largenum
@

AS_CHAR
-------
123456
012345
001234
000123
000012
000001
000000
000000

8 record(s) selected.

--
Something like:

select substr(cast(lva l as char(22)), 22 - length(strip(ca st(lval as
char(22)), L, '0')) + 1, 6) as as_char from largenum"

AS_CHAR
-------
123456
123456
123456
123456
123456
123456
123456
123456

/Lennart
Apr 11 '08 #5

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

Similar topics

2
4152
by: Iona | last post by:
okay.. this should be long.. I made up a table in database with access consists of columns with text type. Some of them I put in data with numeric value and some of them I put in "Unlimited" as the value. Then, I allowed people to insert data but only with conditions. They can insert data if the value they are trying to insert is smaller than the one in the database. For "unlimited" value, they can insert any value. My problem is.... if...
5
13870
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
3
1717
by: eric_caron_31 | last post by:
Here's my problem, I read numeric value and I want to display this value like this : 123 678. Value read : 123678 Value display : 123 678 I want space for separator thanks for your help
13
14996
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
1
1217
by: karthik2423 | last post by:
Hi, Please let me know how I can accomplish the following task - "Ask the user to enter a number". Check whether the user has entered only numeric values. If he/she enters any character other than 0-9, it should print out an error. This is the program I have written so far.. #!/usr/bin/perl use strict; use warnings;
6
2926
by: frohlinger | last post by:
Hi, I need to perform some numeric calculations on a numeric float value, that is received as wstring. I would like to perform a check before converting the wstring to float, checking that indeed the wstring contains a numeric value. This is the actual conversion: double fValue = _wtof(strValue.c_str()); if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0, therefore I do not really know that the input was initially...
2
2513
by: tron_23 | last post by:
hi, we use Toplink (TopLink - 4.6.0 (Build 417) with a DB2 Database 7.2. i know really old versions, but we could change to e newer one ;-) Sometimes we got some problems with update or insert statements -- numeric value out of range. The SQL statements should be right. We took the statements and we tried to execute it manually. It works... in our programm sometimes it crashed and we get a Database Exception: CLI0111E Numeric value...
13
4144
by: arial | last post by:
Hi all, I need some help from you expert. I need to extract numeric value of a string. for ext: string str = "1234 absc st" or it can be "123 hello world" or "1234567 asp.net" How can i only get the numeric part of the string?
1
1966
by: deepab1983 | last post by:
Hai, My XML Data is as follows: <TAXDETAIL> <EACHTAX> <TAXNAME>CST 12.5 % On Equipments</TAXNAME> <TAXVALE>1091.25</TAXVALE> </EACHTAX> <EACHTAX>
0
9969
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9810
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9612
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7999
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7151
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4650
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
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3257
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.