• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL Question - setting a fixed record length using Spool

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL Question - setting a fixed record length using Spool

    I'm trying to use the SQL spool command to set a max record length for the output file I'm spooling to. However, when the data exceeds the max record length, it just wraps the data onto the next record.

    I'm looking for a way of stopping it writing the extra record, I just want it to truncate the data at the max record length. I can't see an obvious way to do this, and being new to SQL I can't work out the best way to do it. Any advice would be appreciated. Thanks.

    For example:

    CLEAR BREAKS
    SET HEADING OFF
    SET LINESIZE 71
    SET ECHO OFF
    SET SERVEROUTPUT OFF
    SET TERMOUT OFF
    SET FEEDBACK OFF
    SET PAGESIZE 0

    SPOOL D:\Data\outputfile.dat
    SELECT *
    FROM table_name;
    SPOOL OFF
    /
    EXIT

    #2
    Can you use CAST instead?
    ‎"See, you think I give a tulip. Wrong. In fact, while you talk, I'm thinking; How can I give less of a tulip? That's why I look interested."

    Comment


      #3
      I think I've done it. Instead of this:

      SELECT *
      FROM table_name;


      do this:

      SELECT SUBSTR(column_name,1,71)
      FROM table_name;


      which I think works because there's only one column in the table.

      Comment


        #4
        Yep, that would do it.
        ‎"See, you think I give a tulip. Wrong. In fact, while you talk, I'm thinking; How can I give less of a tulip? That's why I look interested."

        Comment


          #5
          You might be able to do it using a COLUMN ... FORMAT modifier along with SET WRAP.

          SET WRAP OFF
          COLUMN column_name FORMAT A71


          I've not tried this so, as usual, I could be wrong.

          Comment


            #6
            Originally posted by Moose423956 View Post
            I think I've done it. Instead of this:

            SELECT *
            FROM table_name;


            do this:

            SELECT SUBSTR(column_name,1,71)
            FROM table_name;


            which I think works because there's only one column in the table.
            That will do it if there is only one column in the table.

            Otherwise:

            SELECT SUBSTR(col1||','||col2||','||col3,1,71)
            FROM foo;


            would give you the first 71 characters of coumn1,2 and 3 separated by a comma.
            Best Forum Advisor 2014
            Work in the public sector? You can read my FAQ here
            Click here to get 15% off your first year's IPSE membership

            Comment


              #7
              SELECT SUBSTR(col1||','||col2||','||col3,1,71)
              FROM foo;
              Am I right in thinking this is potentially misleading? - if there are 71 characters in column 1 then you lose the data from columns 2 and 3.

              SELECT SUBSTR(col1,1,23)||','||SUBSTR(col2,1,24)||','||SU BSTR(col3,1,24)
              FROM foo;

              (or whichever length you wanted to take from each field)
              Speaking gibberish on internet talkboards since last Michaelmas. Plus here on Twitter

              Comment


                #8
                Just use a combo of substr (for knocking characters off) and RPAD (for adding spaces on) to fix the record at 70 characters (or whatever it is).

                So

                select RPAD(substr(column,1,72),72) from tablex

                Comment


                  #9
                  In SQL Server 2000 the following works:

                  select LEFT(columnname, 71)
                  from tablename

                  SUBSTR gives a syntax error, and you need SUBSTRING instead.

                  Comment


                    #10
                    Originally posted by TheFaQQer View Post
                    SELECT SUBSTR(col1||','||col2||','||col3,1,71)
                    FROM foo;
                    Originally posted by MrMark View Post
                    Am I right in thinking this is potentially misleading? - if there are 71 characters in column 1 then you lose the data from columns 2 and 3.

                    SELECT SUBSTR(col1,1,23)||','||SUBSTR(col2,1,24)||','||SU BSTR(col3,1,24)
                    FROM foo;

                    (or whichever length you wanted to take from each field)
                    No, it's not misleading - the requirement was to output no more than 71 characters, which this does. If there are more than 71 characters in the concatenated columns, then these will not appear.
                    Best Forum Advisor 2014
                    Work in the public sector? You can read my FAQ here
                    Click here to get 15% off your first year's IPSE membership

                    Comment

                    Working...
                    X