473,809 Members | 2,805 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 24650
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
4149
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
13862
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
1716
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
14988
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
1215
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
2923
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
4141
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
1963
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
9603
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
10643
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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
7664
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
6881
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();...
0
5550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3862
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.