Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Empty Strings added as DBNULL instead of " " #9

Open
jakedenyer opened this issue Jan 26, 2016 · 3 comments
Open

Enhancement: Empty Strings added as DBNULL instead of " " #9

jakedenyer opened this issue Jan 26, 2016 · 3 comments

Comments

@jakedenyer
Copy link

Warren -

Just noticed this when working with RVTools data from Excel and importing it to a database. Have some empty columns that are adding as empty strings in the DB instead of NULL.

Here is the line I modified to add a DBNULL value instead of an empty string.

                if ($property.GetType().IsArray)
                {
                    $DR.Item($Name) = $Value | ConvertTo-XML -As String -NoTypeInformation -Depth 1
                }
                elseif($Value -eq $null)
                {
                    $DR.Item($Name) = [DBNull]::Value
                }
                else
                {
                    $DR.Item($Name) = $Value
                }

Change to: elseif($Value -eq $null -or $Value -eq "")

                if ($property.GetType().IsArray)
                {
                    $DR.Item($Name) = $Value | ConvertTo-XML -As String -NoTypeInformation -Depth 1
                }
                elseif($Value -eq $null -or $Value -eq "")
                {
                    $DR.Item($Name) = [DBNull]::Value
                }
                else
                {
                    $DR.Item($Name) = $Value
                }
@RamblingCookieMonster
Copy link
Owner

Thanks for the heads up Jake!

Is this in Out-DataTable?

I think that would make sense (or even a regex for whitespace). Can you think of any datasets where a space would be needed? Wonder if a switch 'WhitespaceIsNotNull' would make sense - convert whitespace to dbnull unless that is specified?

Thanks!

Warren

@jakedenyer
Copy link
Author

Correct. I'm using it in conjuction with invoke-sqlbulkcopy.

I'm sure the .NET class of IsNullOrEmpty would be appropriate in this case.
I just needed a quick fix.

I can't think of a reason for white space in a db. It would really cause
issues because it would take up storage space - although minimal might add
up.

And in powershell it would be tough to tell the difference between DBNULL
and "" - unless you check the type.

Really appreciate all your work man. Your stuff has helped me more than you
know.
On Jan 27, 2016 13:07, "Warren F." [email protected] wrote:

Thanks for the heads up Jake!

Is this in Out-DataTable?

I think that would make sense (or even a regex for whitespace). Can you
think of any datasets where a space would be needed? Wonder if a switch
'WhitespaceIsNotNull' would make sense - convert whitespace to dbnull
unless that is specified?

Thanks!

Warren


Reply to this email directly or view it on GitHub
#9 (comment)
.

@DevOpsDBAMaverick
Copy link

I can confirm that I also had the empty string issue @jakedenyer was referring to and by adding this additional check:
elseif ($null -eq $Value -or $Value -eq "")
resolves the issue when writing to a database table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants