Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect SUBSTRING usage in debug section in sp_BlitzCache and sp_BlitzIndex #3406

Closed
kiloscheffer opened this issue Dec 11, 2023 · 5 comments · Fixed by #3407
Closed

Incorrect SUBSTRING usage in debug section in sp_BlitzCache and sp_BlitzIndex #3406

kiloscheffer opened this issue Dec 11, 2023 · 5 comments · Fixed by #3407

Comments

@kiloscheffer
Copy link

Version of the script
8.17

What is the current behavior?
sp_BlitzCache and sp_BlitzIndex have a section like this:

IF @Debug = 1
BEGIN
    PRINT SUBSTRING(@sql, 0, 4000);
    PRINT SUBSTRING(@sql, 4000, 8000);
    PRINT SUBSTRING(@sql, 8000, 12000);
    PRINT SUBSTRING(@sql, 12000, 16000);
    PRINT SUBSTRING(@sql, 16000, 20000);
    PRINT SUBSTRING(@sql, 20000, 24000);
    PRINT SUBSTRING(@sql, 24000, 28000);
    PRINT SUBSTRING(@sql, 28000, 32000);
    PRINT SUBSTRING(@sql, 32000, 36000);
    PRINT SUBSTRING(@sql, 36000, 40000);
END;

What is the expected behavior?
The syntax for SUBSTRING is

SUBSTRING ( expression, start, length )

and the numbering of start is 1-based, so the section should probably be

IF @Debug = 1
BEGIN
    PRINT SUBSTRING(@sql, 1, 4000);
    PRINT SUBSTRING(@sql, 4001, 4000);
    PRINT SUBSTRING(@sql, 8001, 4000);
    PRINT SUBSTRING(@sql, 12001, 4000);
    PRINT SUBSTRING(@sql, 16001, 4000);
    PRINT SUBSTRING(@sql, 20001, 4000);
    PRINT SUBSTRING(@sql, 24001, 4000);
    PRINT SUBSTRING(@sql, 28001, 4000);
    PRINT SUBSTRING(@sql, 32001, 4000);
    PRINT SUBSTRING(@sql, 36001, 4000);
END;
@BrentOzar
Copy link
Member

BrentOzar commented Dec 11, 2023 via email

@kiloscheffer
Copy link
Author

It's causing no problems. I just stumbled upon this code while searching for something else. But the debug output would be weird.

Here is an example of similar code:

DECLARE @string nvarchar(10) = N'1234567890';

PRINT SUBSTRING(@string, 0, 2);
PRINT SUBSTRING(@string, 2, 4);
PRINT SUBSTRING(@string, 4, 6);
PRINT SUBSTRING(@string, 6, 8);
PRINT SUBSTRING(@string, 8, 10);

which outputs

1
2345
456789
67890
890

What is intended here is probably:

DECLARE @string nvarchar(10) = N'1234567890';

PRINT SUBSTRING(@string, 1, 2);
PRINT SUBSTRING(@string, 3, 2);
PRINT SUBSTRING(@string, 5, 2);
PRINT SUBSTRING(@string, 7, 2);
PRINT SUBSTRING(@string, 9, 2);

which outputs

12
34
56
78
90

@kiloscheffer
Copy link
Author

It is possible that a pull request would have been more suitable in this instance rather than a bug report; however, my familiarity with GitHub is still limited.

Montro1981 added a commit to Montro1981/SQL-Server-First-Responder-Kit that referenced this issue Dec 14, 2023
@Montro1981
Copy link
Contributor

@perscheffer Thanks for the report, I added PR #3407 to fix this issue.

BrentOzar added a commit that referenced this issue Dec 14, 2023
…it_3406

#3406 Fixed the Substring command to respect the command syntax
@BrentOzar BrentOzar added this to the 2023-12 Release milestone Dec 14, 2023
@BrentOzar
Copy link
Member

Looks good! Thanks for the pull request. Merging into the dev branch, will be in the next release with credit to you in the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants