Format Output

  • Can someone tell me please the syntax to format the output of a query such that the text from a column wraps after a certain number of characters?

    Thanks!


    SELECT * FROM users WHERE clue > 0
    0 rows returned.

  • You cannot wrap it, however if you want to have a psuedo wrap and are using sql 2000 you could create a function to stuff a char(13) + char(10) every so many characters and even determine if this would chop a word and back up. However, you are better off doing this in a client with a control that handles clipping. Where are you trying to do this?

  • Yup, I realize this would be a kludge but I am getting desperate. The application is an ASP.NET page, the control that displays the data is a DataGrid - should wrap automatically. Previous records do wrap automatically. New records added to the database do not wrap and cause the control to ignore it's width property. I have been debugging this problem for a couple days without success.

    Cheers!

    SELECT * FROM users WHERE clue > 0

    0 rows returned.


    SELECT * FROM users WHERE clue > 0
    0 rows returned.

  • quote:


    Here's a function patterned after the UNIX fold utility

    
    
    CREATE FUNCTION Fold (
    @pMaxWidthsmallint,
    @pStringvarchar(7800)
    )
    RETURNS varchar(8000)

    BEGIN

    DECLARE @FmtStringvarchar(8000),
    @Startsmallint,
    @Ptrsmallint

    SET @FmtString = ''
    SET @Start = 1

    WHILE @Start + @pMaxWidth < LEN(@pString)
    BEGIN
    SET @Ptr = CHARINDEX(' ', REVERSE(SUBSTRING(@pString, @Start, @pMaxWidth)), 1)
    SET @FmtString = @FmtString + SUBSTRING(@pString, @Start, @pMaxWidth - @Ptr + 1)+ CHAR(13) + CHAR(10)
    SET @Start = @Start + @pMaxWidth - @Ptr + 1
    END

    SET @FmtString = @FmtString + SUBSTRING(@pString, @Start, @pMaxWidth)

    RETURN @FmtString

    END

    GO

    SELECT dbo.fold(40, 'The behind-the-scenes lobbying is paying dividends. The administration is reconsidering its support for a plan requiring publicly traded companies to describe their hacker defenses to securities regulators. ')

  • This is awesome, thanks!

    SELECT * FROM users WHERE clue > 0

    0 rows returned.


    SELECT * FROM users WHERE clue > 0
    0 rows returned.

Viewing 5 posts - 1 through 4 (of 4 total)

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