Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlcmd - How to get around column length limit without empty spaces?

I'm trying to use sqlcmd on a windows machine running SQL Server 2005 to write a query to a csv file. The command line options we typically use are:

-l 60 -t 300 -r 1 -b -W -h -1

However, the columns are getting truncated at 256 bytes. In an attempt to circumvent this, I tried using this command line option in place of -W:

-y 8000

This captures the entire fields, but the problem with this method is that the file balloons up from just over 1mb to about 200mb due to all the extra space (I realize 8000 is probably overkill, but it will probably have to be at least 4000 and I'm currently only working with a small subset of data). The -W option typically eliminates all this extra space, but when I try to use them together it tells me they're mutually exclusive.

Is there a way to get sqlcmd around this limit, or does anyone know if another program (such as bcp or osql) would make this easier?


Edit: Here are the code snippets we're using to get the field that's being truncated (similar code is used for a bunch of fields):

SELECT ALIASES.AliasList as complianceAliases,    

...

LEFT OUTER JOIN (Select M1.ID, M1.LIST_ID,stuff((SELECT '{|}' + isnull(Content2,'')+' '+isnull(Content3,'')+' '+isnull(Content4,'')+' '+isnull(Content5,'')+' '+isnull(Content6,'')+' '+isnull(Content7,'')
                                  FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M2 with (nolock)
                                  WHERE M1.LIST_ID = M2.LIST_ID and M1.ID = M2.ID and M1.TYPE = M2.TYPE                                      
                                  FOR XML PATH('')
                                  ),1,1,'') as AliasList
             FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M1 with (nolock)
             WHERE M1.LIST_ID = 2001 AND M1.TYPE = 'Aliases'
             GROUP BY m1.list_id,m1.ID,m1.Type) as ALIASES
             ON ALIASES.LIST_ID = PAIR.COMP_LIST_ID AND ALIASES.ID = PAIR.COMP_ID
like image 767
Tony Trozzo Avatar asked May 06 '11 20:05

Tony Trozzo


People also ask

How do I find the maximum data length of a column in SQL?

Use the built-in functions for length and max on the description column: SELECT MAX(LEN(DESC)) FROM table_name; Note that if your table is very large, there can be performance issues.

How do you define the length of a column in SQL?

Use COL_LENGTH() to Get a Column's Length in SQL Server In SQL Server, you can use the COL_LENGTH() function to get the length of a column. More specifically, the function returns the defined length of the column, in bytes. The function accepts two arguments: the table name, and the column name.


1 Answers

I ended up solving this by using the "-y0" argument. It still left a bunch of whitespace but it looks like it only went to the end of the longest piece of data in each field.

I then ran the output through a program that removed repeating spaces and that solved all of the problems.

like image 50
Tony Trozzo Avatar answered Nov 07 '22 22:11

Tony Trozzo