jtrapat1
asked on
Insert Into DB2 VarChar Field
I need to know the best way to insert data into a varchar field in a db2 table to optimize space and performance.
Here's how the db2 field is defined:
-------------------------- ------
struct
{
short length;
char data[400];
} purpose;
char purpose_0[400+1];
-------------------------- -------
Now, I'm trying to insert from a C Program and the dba tells me that there is a lot of wasted space when I do my insert.
Here's my C code:
I'm reading from a struct and copying to a host variable.
-------------------------- ---------- ------
strcpy(bp12.purpose,adds.p urpose);
-------------------------- ----
My adds.purpose field is defined as
char purpose[240]
so you can see that there is a lot of space empty after the insert.
-------------------------- ----
Sometimes the value inserted is blank or spaces and it still fills up the entire varchar field.
What is the best way to do this insert from C?
Or, are there some string functions I could use to strip away the trailing spaces before inserting the value?
Thanks in Advance
John
Here's how the db2 field is defined:
--------------------------
struct
{
short length;
char data[400];
} purpose;
char purpose_0[400+1];
--------------------------
Now, I'm trying to insert from a C Program and the dba tells me that there is a lot of wasted space when I do my insert.
Here's my C code:
I'm reading from a struct and copying to a host variable.
--------------------------
strcpy(bp12.purpose,adds.p
--------------------------
My adds.purpose field is defined as
char purpose[240]
so you can see that there is a lot of space empty after the insert.
--------------------------
Sometimes the value inserted is blank or spaces and it still fills up the entire varchar field.
What is the best way to do this insert from C?
Or, are there some string functions I could use to strip away the trailing spaces before inserting the value?
Thanks in Advance
John
ASKER
mglxxx,
Thanks for the function.
Can you tell me?
Now that I have the exact length of the string, do I need to insert both the length AND the string into the struct?
I know we did something similar with cobol but I've never dealt with varchars in C.
Thanks
John
Thanks for the function.
Can you tell me?
Now that I have the exact length of the string, do I need to insert both the length AND the string into the struct?
I know we did something similar with cobol but I've never dealt with varchars in C.
Thanks
John
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The last paragraph in the previous post referred to
the case when you use a zero terminated string.
the case when you use a zero terminated string.
/*
strips trailing blanks from str and returns the new length
*/
int rtrim(char *str) {
int i;
if ( str == NULL )
return -1;
for ( i = strlen(str) - 1;
i >= 0 && isspace(str[i]);
i++ )
str[i] = '\0';
return i + 1;
}