Ask the Expert

Picking the first 25 characters in SQL

The column in database is a varchar(25). But sometimes my datafile has data for this column exceeding 25. I want the SQLloader to take the first 25 characters and load them into the database. I am using a control file, variable-length delimited by |, so I can not mention positions for all the fields, this field represents 40th column in database (total 50 columns). Is there any way to specify to pick the first 25 characters?

    Requires Free Membership to View

Yes. You can do this. The way to accomplish this task is to apply an SQL operator to your field. To use just the first 25 characters, you will want to use the SUBSTR function on the input data. Something similar to the following can help:

LOAD DATA 
INFILE * 
APPEND INTO TABLE XXX 
( LAST    position(1:7)     char   "UPPER(:LAST)" 
  FIRST   position(8:45)    char  
"SUBSTR(:FIRST,1,25)" 
) 

The above is just a portion of what you control file might look like. I've foced the data that will go into the LAST column to be all upper case and I've forced the data to be placed into the FIRST column to be just the first 25 characters of the data.

This was first published in September 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: