Skip to content

Instantly share code, notes, and snippets.

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