0

I've searched for a number of hours now and am unable to figure out how to do this.

I query an MSSQL database that returns 2 columns, one of these values is empty/null but does represent something in the SQL database(I've tested disabling it).

How would I check through what is returned from my query for the empty value and modify this to something else?

$TestQuery = Invoke-Sqlcmd -Database $DB -Query $qcd -ServerInstance "SomeInstance\Instance1" -Verbose

Result:

Activity              Setting
--------              -------
All Operation         Enabled
Backup                Enabled
Restore               Enabled
Prune                 Enabled
Aux Copy              Enabled
Schedule              Enabled
Archive Check         Enabled
Tape Erase            Enabled
Offline content Index Enabled
Online Content Index  Enabled
                      Enabled

You can see the last item returned doesn't have a value but does reflect a setting in the application we use, I just want to modify that value to "Value1" for example.

Any help is greatly appreciated, I did try using hashtables but had no idea what I was doing despite several hours of googling.

Edit: My Query:

SELECT JM.opName AS 'Activity',
    CASE action
    WHEN 1 THEN 'Disabled'
    WHEN 2 THEN 'Enabled'
    END AS 'Setting'
    FROM JMJobAction AS J
    LEFT JOIN JMJobOperationNames JM on JM.opType = J.opType
WHERE clientId = 1
    AND appType = 0
    AND J.opType != 8
    AND appId = 1

2 Answers 2

1

You may do the following in PowerShell:

$TestQuery = Invoke-Sqlcmd -Database $DB -Query $qcd -ServerInstance "SomeInstance\Instance1"
$TestQuery |
    Where { [string]::IsNullOrEmpty($_.Activity) } | Foreach-Object {
        $_.Activity = 'Value1' # Update all empty or nulls with Value1
    }
$TestQuery # Contains updated results

Note that this does not update the actual database. You will need a separate query that writes back to the database.


When a database table contains a NULL, it is interpreted as the System.DBNull data type in PowerShell. [System.DBNull]::Value is not the same as $null. So if you only wanted to query for NULL values, then your query could more appropriately be modified to the following:

$TestQuery | Where Activity -is [DBNUll]
Sign up to request clarification or add additional context in comments.

1 Comment

I did read somewhere that a DB Null is not the same as $null. But thank you so much, this has resolved the issue I was having, I couldn't figure out how to do this within the SQL query but this is the next best thing.
1

I don't know if I understand your question correctly. I understand that you want to have a default_value when there is no data in a column.

That can be solved in your SQL Query with case. Here an example

[Edit] Based on your added query

SELECT 
    CASE 
      WHEN JM.opName is null OR JM.opName = '' THEN "DefaultActivity" 
      ELSE JM.opName
    END AS Activity,
    CASE action
       WHEN 1 THEN 'Disabled'
       WHEN 2 THEN 'Enabled'
    END AS 'Setting'
FROM JMJobAction AS J
    LEFT JOIN JMJobOperationNames JM on JM.opType = J.opType
WHERE clientId = 1
    AND appType = 0
    AND J.opType != 8
    AND appId = 1

2 Comments

So I'm not sure how to add this into my query, I've added my query into my first post I just keep breaking the query
Hi @Mike, I have updated my answer. Unfortunately I cannot test it, I dont have such database :) but give this a try and let me know if there are errors

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.