Skip to content

Instantly share code, notes, and snippets.

@shaneis
Created August 10, 2017 09:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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