473,498 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inserting \0 into a CHAR/VARCHAR field

Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek

Dec 4 '06 #1
13 8427

Vivek wrote:
Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek
For example:
'ABC' || CHR(0) || CHR(10) || CHR(13) || 'XYZ'

Dec 5 '06 #2
Tonkuma wrote:
>
Vivek wrote:
>Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek
For example:
'ABC' || CHR(0) || CHR(10) || CHR(13) || 'XYZ'
Unfortunately, this won't work because CHR(0) = 0x20 = ' ' = space.
http://publib.boulder.ibm.com/infoce...n/r0000778.htm

But an 0x00 = '\0' can be inserted by an application.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 5 '06 #3
Knut,

What you said is right, but how is chr(0) mapped to 0x20 ?
i thought chr() took ascii values and 0 is the ascii for '\0'
character.

Knut Stolze wrote:
Tonkuma wrote:

Vivek wrote:
Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek
For example:
'ABC' || CHR(0) || CHR(10) || CHR(13) || 'XYZ'

Unfortunately, this won't work because CHR(0) = 0x20 = ' ' = space.
http://publib.boulder.ibm.com/infoce...n/r0000778.htm

But an 0x00 = '\0' can be inserted by an application.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 5 '06 #4
Vivek wrote:
Knut,

What you said is right, but how is chr(0) mapped to 0x20 ?
i thought chr() took ascii values and 0 is the ascii for '\0'
character.
The explanation is here, i.e. in the description of the CHR function that I
already cited:
http://publib.boulder.ibm.com/infoce...n/r0000778.htm

As for the "how", I assume that a simple test will cover the 0 input inside
the DB2 engine:

if (input == 0) {
output = ' ';
}
else {
output = input;
}

(I don't think that this was really your question?)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 5 '06 #5
x'00' - should do the trick.
db2 values ascii(x'00')
1
-----------
0

1 record(s) selected.

regards,
dmitri

"Vivek" <te*********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek

Dec 5 '06 #6
SVK

"""Knut Stolze ():
"""
>
Unfortunately, this won't work because CHR(0) = 0x20 = ' ' = space.
http://publib.boulder.ibm.com/infoce...n/r0000778.htm
1) It clearly says that CHR(0) = 0x00 in ASCII.
0x20 - is a senseless bullshit, nothing like this can be found anywhere

2) constant '00'X can be used for the same purpose

Dec 5 '06 #7
SVK wrote:
>
"""Knut Stolze писал(а):
"""
>>
Unfortunately, this won't work because CHR(0) = 0x20 = ' ' = space.
http://publib.boulder.ibm.com/infoce...n/r0000778.htm
>>

1) It clearly says that CHR(0) = 0x00 in ASCII.
0x20 - is a senseless bullshit, nothing like this can be found anywhere
Sorry, I put the wrong link in there. It should have been:

http://publib.boulder.ibm.com/infoce...c/r0000778.htm

V8 has the exact same behavior as V9. Unfortunately, the V8 docs are not
detailed enough in this respect and do not mention the special treatment of
0. If you try the following SQL statement on a V8 system, you will surely
agree with me:

VALUES HEX(CHR(0))
2) constant '00'X can be used for the same purpose
This is correct:

VALUES HEX('abc' || x'00' || 'def')

1
--------------
61626300646566

The only thing to remember in all this is that 0x00 = '\0' is often the
end-of-line character. The DB2 command line interprets it as such and,
thus, you may not get the full output:

VALUES VARCHAR('abc' || x'00' || 'def')

1
-------
abc

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 6 '06 #8
My question actually should have been "why" is it mapped that way.
chr(0) should be 0x00

Knut Stolze wrote:
Vivek wrote:
Knut,

What you said is right, but how is chr(0) mapped to 0x20 ?
i thought chr() took ascii values and 0 is the ascii for '\0'
character.

The explanation is here, i.e. in the description of the CHR function that I
already cited:
http://publib.boulder.ibm.com/infoce...n/r0000778.htm

As for the "how", I assume that a simple test will cover the 0 input inside
the DB2 engine:

if (input == 0) {
output = ' ';
}
else {
output = input;
}

(I don't think that this was really your question?)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 6 '06 #9
The behaviour is definitely as you said.
I was able to insert the '\0' character as follows:

insert into table values(x'00')

and

select * from table : gave

----
00

Knut Stolze wrote:
SVK wrote:

"""Knut Stolze писал(а):
"""
>
Unfortunately, this won't work because CHR(0) = 0x20 = ' ' = space.
http://publib.boulder.ibm.com/infoce...n/r0000778.htm
>
1) It clearly says that CHR(0) = 0x00 in ASCII.
0x20 - is a senseless bullshit, nothing like this can be found anywhere

Sorry, I put the wrong link in there. It should have been:

http://publib.boulder.ibm.com/infoce...c/r0000778.htm

V8 has the exact same behavior as V9. Unfortunately, the V8 docs are not
detailed enough in this respect and do not mention the special treatment of
0. If you try the following SQL statement on a V8 system, you will surely
agree with me:

VALUES HEX(CHR(0))
2) constant '00'X can be used for the same purpose

This is correct:

VALUES HEX('abc' || x'00' || 'def')

1
--------------
61626300646566

The only thing to remember in all this is that 0x00 = '\0' is often the
end-of-line character. The DB2 command line interprets it as such and,
thus, you may not get the full output:

VALUES VARCHAR('abc' || x'00' || 'def')

1
-------
abc

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 6 '06 #10
Vivek wrote:
The behaviour is definitely as you said.
I was able to insert the '\0' character as follows:

insert into table values(x'00')

and

select * from table : gave

----
00
The result does not match with what you have done. Either your query was
actually SELECT HEX(...) FROM table or your insert was different. x'00' is
a 0x00 byte. Your results show a string with two 0-s in it.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 6 '06 #11
Vivek wrote:
My question actually should have been "why" is it mapped that way.
chr(0) should be 0x00
I don't know. Probably some sort of historical reasons.

Storing non-printable characters in a VARCHAR is usually not a good idea in
the first place. If it is binary, you should use VARCHAR FOR BIT DATA and
treat it as binary data.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 6 '06 #12
Vivek wrote:
Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek
A bit of explanation:

Online documentation available at:
http://publib.boulder.ibm.com/infoce...w/v9/index.jsp

contains this (search for CHR in Version 8 incompatibilities):
http://publib.boulder.ibm.com/infoce...63%68%72%22%20

Output from CHR function

Change
CHR(0) returns a blank (X'20') instead of the character with code point X'00'.

Symptom
Output from the CHR function with X'00' as the argument returns different results.

Explanation
String handling when invoking and returning from user-defined functions
interprets X'00' as end of string.

Resolution
Change the application code to handle the new output value. Alternatively,
define a user-defined function that returns CHAR(1) FOR BIT DATA which is
sourced from the definition of the SYSFUN CHR function, and place this function
before SYSFUN on the SQL path.
For example, to find the source definition for SYSFUN.CHR located in column
IMPLEMENTATION:

SELECT IMPLEMENTATION, ROUTINENAME FROM SYSIBM.SYSROUTINES WHERE ROUTINENAME
LIKE '%CHR%';

IMPLEMENTATION ROUTINENAME
-------------- -----------
db2clifn!CLI_udfCHAR CHR
Then, you could create a new user-defined function from the definition
db2clifn!CLI_udfCHAR returned above.

CREATE FUNCTION DBS.CHR(INTEGER) RETURNS CHARACTER(1) FOR BIT DATA NOT FENCED
LANGUAGE C PARAMETER STYLE DB2SQL NO DBINFO EXTERNAL NAME db2clifn!CLI_udfCHAR'
You can also use Dmitri's suggestion of using x'00' instead of CHR(0)
Jan M. Nelken
Dec 7 '06 #13
Vivek wrote:
Hi,

Is it possible to insert a '\0' value or for that matter any control
character into a DB2 CHAR/VARCHAR field ? So if i do a 'select
hex(column) from table' i should see a 0 in the output. How can i do it
from the command prompt ?

Thanks,
Vivek
A bit of explanation:

Online documentation available at:
http://publib.boulder.ibm.com/infoce...w/v9/index.jsp

contains this (search for CHR in Version 8 incompatibilities):
http://publib.boulder.ibm.com/infoce...63%68%72%22%20

Output from CHR function

Change
CHR(0) returns a blank (X'20') instead of the character with code point X'00'.

Symptom
Output from the CHR function with X'00' as the argument returns different results.

Explanation
String handling when invoking and returning from user-defined functions
interprets X'00' as end of string.

Resolution
Change the application code to handle the new output value. Alternatively,
define a user-defined function that returns CHAR(1) FOR BIT DATA which is
sourced from the definition of the SYSFUN CHR function, and place this function
before SYSFUN on the SQL path.
For example, to find the source definition for SYSFUN.CHR located in column
IMPLEMENTATION:

SELECT IMPLEMENTATION, ROUTINENAME FROM SYSIBM.SYSROUTINES WHERE ROUTINENAME
LIKE '%CHR%';

IMPLEMENTATION ROUTINENAME
-------------- -----------
db2clifn!CLI_udfCHAR CHR
Then, you could create a new user-defined function from the definition
db2clifn!CLI_udfCHAR returned above.

CREATE FUNCTION DBS.CHR(INTEGER) RETURNS CHARACTER(1) FOR BIT DATA NOT FENCED
LANGUAGE C PARAMETER STYLE DB2SQL NO DBINFO EXTERNAL NAME db2clifn!CLI_udfCHAR'
You can also use Dmitri's suggestion of using x'00' instead of CHR(0)
Jan M. Nelken
Dec 7 '06 #14

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

Similar topics

2
2472
by: Scott | last post by:
I have a stored procedure where I want to select all fields matching the query into another table. In addition, I want to add a common groupID to each of the records that are being inserted into...
10
70991
by: Techie | last post by:
what's the difference of the two data types?
10
1836
by: ryan.mclean | last post by:
Hi all, I am new to using sql server and parameterized sql. I am hoping to be returned the value of a column that has been inserted. Here is my statement strSqlInsetrtTrack = _ "INSERT INTO...
15
18860
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
5
2176
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
15
2881
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
2
4707
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
11
4346
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store...
3
1361
by: Tradeorganizer | last post by:
Hi, I have a database with table name as test in that i have 6 colums they are name varchar (20) address varchar (20) position varchar (20) nametype1 varchar (20) nametype2 varchar (20)
0
7121
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,...
0
6993
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...
0
7197
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...
1
6881
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...
0
7375
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...
0
5456
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 projectplanning, coding, testing,...
0
4584
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...
0
1411
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 ...
1
650
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.