Using SQLCMD, How to Specify Column Length for Output?

  • hi experts,

    This is 2005.

    This code generates a report - works fine.

    But I want to reduce the column width so each line will be on 1 line when the report is printed.

    When I try SELECT CAST(ServerName varchar(15)) as Server,

    I get error "Incorrect syntax near 'varchar'."

    How can I trim the column width on the output?

    Thanks, John

    SET @sqlcmd = 'sqlcmd -Q '

    SET @cmd = '"SELECT CAST(ServerName varchar(15)) as Server, LoginName as FailedLogin, ClientName as ComputerName, LoginDate from DBA.dbo.AuditDatabaseLogins WHERE AuditType = ''Login Failed'' AND LoginDate between ''' + convert(varchar(10), @PriorMonthStart, 112) + ''' and ''' + convert(varchar(10), @PriorMonthEnd, 112) + ''' " -o C:\AuditReports\TestSqlCmd35.txt'

    SET @sqlcmd = @sqlcmd + @cmd;

    Print @sqlcmd;

    EXEC xp_cmdshell @sqlcmd,no_output;

  • SELECT CAST(ServerName [font="Arial Black"]AS[/font] varchar(15)) as Server,

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply