0

Below is code snippet from Powershell

$query =  "select MAX(convert(varchar(10),server_time,102))from status_history where status not like ('ERROR FETCHING%')" 
$servers = Invoke-Sqlcmd2 -query $query -ServerInstance "100.81.60.2" -Database "temp" -Username "temp" -password "temp"

I need to check in Powershell if the sql query contained in $query returns NULL. I tried using

if($servers.Column1 -eq NULL)

but it doesnt seem to work.

2
  • try using if($servers.Column1 -eq $null) or just omit it: if ($servers.Column1) Commented Mar 29, 2016 at 13:20
  • 4
    Using if ($servers.Column1) is not a good idea in this context, because values like 0 or "" (empty string) also evaluate to $false. I'd try [DBNull]::Value.Equals(). Commented Mar 29, 2016 at 13:27

1 Answer 1

2

Compare the result to [DBNull]::Value:

if ([DBNull]::Value.Equals($server.Column1)) {
  ...
}

If $server.Column1 contains more than one value you may need to check each value individually.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.