Why doesn't this work?

  • Hello,

    Just a simple but puzzling question; When I execute the following T-SQL the @MaxLen variable appears to hold the desired value, but when used within the LEFT clause doesn't appear to have any affect. If I replace the @MaxLen variable with a constant of the same value, it works as expected.

    Here's the code:

    DECLARE@MaxLenINT= (SELECT MAX(LEN(Name)) FROM sys.filegroups);

    PRINT @MaxLen;

    SELECTLEFT(name,@MaxLen) ,

    CASE is_read_only

    WHEN 0 THEN 'Read Write'

    WHEN 1 THEN 'Read Only'

    ELSE 'Unknown'

    END AS AccessMode

    FROMsys.filegroups

    WHen I run this the 'Name' column is not truncated to the length specified in the @MaxLen variable.

    Try it for yourself and please let me know if I've done something stupid or this is a genuine oddity.

  • raotor (1/18/2012)


    Hello,

    Just a simple but puzzling question; When I execute the following T-SQL the @MaxLen variable appears to hold the desired value, but when used within the LEFT clause doesn't appear to have any affect. If I replace the @MaxLen variable with a constant of the same value, it works as expected.

    Here's the code:

    DECLARE@MaxLenINT= (SELECT MAX(LEN(Name)) FROM sys.filegroups);

    PRINT @MaxLen;

    SELECTLEFT(name,@MaxLen) ,

    CASE is_read_only

    WHEN 0 THEN 'Read Write'

    WHEN 1 THEN 'Read Only'

    ELSE 'Unknown'

    END AS AccessMode

    FROMsys.filegroups

    WHen I run this the 'Name' column is not truncated to the length specified in the @MaxLen variable.

    Try it for yourself and please let me know if I've done something stupid or this is a genuine oddity.

    Please post it with detailed information. From this information what I see is everthing seems to be ok.

    I think your filegroup name may contain spaces. Please check again.

    Post with actual data and results for others to help you.

    -- Babu

  • Works ok for me:

    DECLARE@MaxLenINT= (SELECT MAX(LEN(Name)) FROM sys.filegroups);

    SELECT

    [name],

    [MaxLen] = @MaxLen,

    [Shortname] = LEFT(name,@MaxLen),

    [ShortLength] = LEN(LEFT(name,@MaxLen)),

    CASE is_read_only

    WHEN 0 THEN 'Read Write'

    WHEN 1 THEN 'Read Only'

    ELSE 'Unknown'

    END AS AccessMode

    FROMsys.filegroups


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • baabhu (1/18/2012)


    raotor (1/18/2012)


    Hello,

    Just a simple but puzzling question; When I execute the following T-SQL the @MaxLen variable appears to hold the desired value, but when used within the LEFT clause doesn't appear to have any affect. If I replace the @MaxLen variable with a constant of the same value, it works as expected.

    Here's the code:

    DECLARE@MaxLenINT= (SELECT MAX(LEN(Name)) FROM sys.filegroups);

    PRINT @MaxLen;

    SELECTLEFT(name,@MaxLen) ,

    CASE is_read_only

    WHEN 0 THEN 'Read Write'

    WHEN 1 THEN 'Read Only'

    ELSE 'Unknown'

    END AS AccessMode

    FROMsys.filegroups

    WHen I run this the 'Name' column is not truncated to the length specified in the @MaxLen variable.

    Try it for yourself and please let me know if I've done something stupid or this is a genuine oddity.

    Please post it with detailed information. From this information what I see is everthing seems to be ok.

    I think your filegroup name may contain spaces. Please check again.

    Post with actual data and results for others to help you.

    -- Babu

    Please find attached two report files.

    Result_1 is the output using the code as above.

    Result_2 is the output with the @MaxLen variable substituted with the constant value of 9 (Which is the longest filegroup name in my result set).

    Also bear in mind that my results are set as "Results To Text" for viewing.

  • This was removed by the editor as SPAM

  • Can't see those report files.

    You are using SQL Server 2998, aren't you?

    John

  • John Mitchell-245523 (1/18/2012)


    Can't see those report files.

    You are using SQL Server 2998, aren't you?

    John

    I believe I'm pretty up to date as I'm using SS2008R2 SP1.

    Did you get the actual attachments? II'm still pretty new at all this, so I may havbe screwed up with the attaching bit! 🙂

  • raotor (1/18/2012)


    Did you get the actual attachments?

    No, there's nothing attached to your previous post. Click on Edit Post and then click on Edit Attachments to start uploading them.

    John

  • I think it should've worked now. There should be a .rar format attachment on this post with the Result_1 and Result_2 report files.

  • raotor (1/18/2012)


    I think it should've worked now. There should be a .rar format attachment on this post with the Result_1 and Result_2 report files.

    I think this is the property of the SQLserver management studio.

    I just realized both the results have same datalenght.

    DECLARE@MaxLentinyint= (SELECT MAX(LEN(Name)) FROM sys.filegroups);

    select 'Max',@MaxLen;

    SELECTdatalength(LEFT(name,convert(int,@MaxLen))) ,CASE is_read_onlyWHEN 0 THEN 'Read Write'WHEN 1 THEN 'Read Only'ELSE 'Unknown'END AS AccessModeFROMsys.filegroups;

    set @MaxLen = 7

    SELECTdatalength(LEFT(name,7)) ,CASE is_read_onlyWHEN 0 THEN 'Read Write'WHEN 1 THEN 'Read Only'ELSE 'Unknown'END AS AccessModeFROMsys.filegroups;

    Both will return the same datalength.

Viewing 10 posts - 1 through 9 (of 9 total)

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