Skip to content

Instantly share code, notes, and snippets.

@shaneis
Created August 10, 2017 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaneis/d64b74488616c5208d9c168376f801ae to your computer and use it in GitHub Desktop.
Save shaneis/d64b74488616c5208d9c168376f801ae to your computer and use it in GitHub Desktop.
Substring Length Error
#... get some results to work with...
$sqlcmdParams = @{
ServerInstance = 'localhost\SQLSERVER2K16'
Database = 'master'
Query = @'
SELECT TOP(10)
name
FROM dbo.spt_values
WHERE name IS NOT NULL
AND LEN(name) != 0
ORDER BY name;
'@
};
$dbResults = Invoke-Sqlcmd @sqlcmdParams;
#...now check the substring function...
foreach ($row in ($dbResults.name)) {
[PSCustomObject]@{
RowName = $row
RowSubString = $row.Substring(5, 100)
};
};
<#
# Error message:
# Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string"
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment