473,769 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pad a sequence value with zeros

Hi,

I Oracle one can have :

select to_char(a_id_se q.nextval,'0000 ') from dual

Which fetches a sequence value and pads it with zeros
on the left. When the sequence value is more than
4 digits '####' is returned.

How do I do about the same thing in DB2 ?

select char(nextval_ for a_schema.a_id,' 0000') doesn't work.

I've tried :

select replicate('0',4-len(A.id)),A.id from (
select nextval for a_schema.a_id_s eq id
from sysibm.sysdummy 1 ) A

But this returns a message saying q_id_seq can not
be specified in this context.

Any ideas or handy functions?

Thanks.

Nov 12 '05 #1
4 14596
gi************* ******@yahoo.co m wrote:
Hi,

I Oracle one can have :

select to_char(a_id_se q.nextval,'0000 ') from dual

Which fetches a sequence value and pads it with zeros
on the left. When the sequence value is more than
4 digits '####' is returned.

How do I do about the same thing in DB2 ?

select char(nextval_ for a_schema.a_id,' 0000') doesn't work.

I've tried :

select replicate('0',4-len(A.id)),A.id from (
select nextval for a_schema.a_id_s eq id
from sysibm.sysdummy 1 ) A

But this returns a message saying q_id_seq can not
be specified in this context.

Any ideas or handy functions?

Thanks.

A language independent trick (modulo various levels of syntactic sugar) is:
substr(char(mod (x, 10000)+10000),2 ,4)
A source of problem is negative x (and how mod treats that).
Nov 12 '05 #2

select char(nextval_ for a_schema.a_id,' 0000') doesn't work.

Maybe 'select right(digits(ne xtval for a_schema.a_id), 4) from
sysibm.sysdummy 1' will work.
Nov 12 '05 #3
Gert van der Kooij wrote:
select char(nextval_ for a_schema.a_id,' 0000') doesn't work.


Maybe 'select right(digits(ne xtval for a_schema.a_id), 4) from
sysibm.sysdummy 1' will work.

or
db2 => values substr(char(cas t(next value for s as decimal(4, 0))), 1, 4)
db2 (cont.) => ;

1
----
0003

Note that for general to_char() support take a look at the function
library of the migration tool kit.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
> Which fetches a sequence value and pads it with zeros
on the left. When the sequence value is more than
4 digits '####' is returned.


------------------------- Commands Entered -------------------------
ALTER SEQUENCE a_id RESTART WITH 1;
--------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------- Commands Entered -------------------------
SELECT NEXT VALUE FOR a_id
, SUBSTR(DIGITS(N EXT VALUE FOR a_id)||'####'
,7+SIGN(SIGN(NE XT VALUE FOR a_id - 10000)+1)*4,4)
FROM SYSIBM.SYSDUMMY 1;
--------------------------------------------------------------------

1 2
----------- ----
1 0001

1 record(s) selected.
------------------------- Commands Entered -------------------------
ALTER SEQUENCE a_id RESTART WITH 9999;
--------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------- Commands Entered -------------------------
SELECT NEXT VALUE FOR a_id
, SUBSTR(DIGITS(N EXT VALUE FOR a_id)||'####'
,7+SIGN(SIGN(NE XT VALUE FOR a_id - 10000)+1)*4,4)
FROM SYSIBM.SYSDUMMY 1;
--------------------------------------------------------------------

1 2
----------- ----
9999 9999

1 record(s) selected.
------------------------- Commands Entered -------------------------
SELECT NEXT VALUE FOR a_id
, SUBSTR(DIGITS(N EXT VALUE FOR a_id)||'####'
,7+SIGN(SIGN(NE XT VALUE FOR a_id - 10000)+1)*4,4)
FROM SYSIBM.SYSDUMMY 1;
--------------------------------------------------------------------
1 2
----------- ----
10000 ####

1 record(s) selected.
------------------------- Commands Entered -------------------------
SELECT NEXT VALUE FOR a_id
, SUBSTR(DIGITS(N EXT VALUE FOR a_id)||'####'
,7+SIGN(SIGN(NE XT VALUE FOR a_id - 10000)+1)*4,4)
FROM SYSIBM.SYSDUMMY 1;
--------------------------------------------------------------------
1 2
----------- ----
10001 ####

1 record(s) selected.

Nov 12 '05 #5

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

Similar topics

5
36777
by: Ken1 | last post by:
I am going to drop a primary key from one column and create a new column to be used as primary key in an existing database. The old column was a date column which someone earlier though was a good candidate for a primary key which we all know it's not. Now I want to add a new field, i.e. called ID, with a normal number sequence as primary key. I have dropped the primary key, created the new column, created the new sequence and created...
21
4210
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A C double can be aliased by an array of unsigned char, to which any desired byte pattern may be written. Is there any somewhat portable way of assigning a byte pattern such that the result will, with very high probability, _not_ count as a valid...
53
4091
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
1
2212
by: Marek Lewczuk | last post by:
Hello, I would like to ask if my problem with sequence is a proper behavior or this is a bug (probably not)... I have a table: CREATE TABLE "testtable" ( "serialfield" SERIAL, "someotherfield" TEXT, PRIMARY KEY("serialfield")
14
9904
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up to 1 . I am not sure how to do this. Any idea? Thanks.
0
938
by: amitsoni.1984 | last post by:
Hi, I am using the code given below where A is a matrix and row is a sequence. But it gives following error: -------- error------ A=row ValueError: setting an array element with a sequence. --------------code---------------- #!/usr/bin/python
8
5654
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some ideas of how to approach the problem (1) reading file into unsigned char buffer, 2) defining bit structure, 3) comparing the first 4 bits of the buffer with the bit structure, 4) shifting the char buffer one left, 5) repeat at step 3)) but I'm...
1
3029
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the array data from 0 to3, I get this {13, JUNK VALUE, 17,38, 18} and JUNK VALUE is like 1.8e831 or something like that. This happens when I use the attach() function and use the current() function to display the values at data I really want to...
6
7764
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for each file that he has us process. He wants this in a Comma Delimited Format.
0
10210
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
10039
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
9990
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
8869
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
7406
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
6668
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
5297
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...
1
3955
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
3
2814
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.