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

Insert object containing DateTime #9

Open
jasoncocks opened this issue Jun 21, 2018 · 3 comments
Open

Insert object containing DateTime #9

jasoncocks opened this issue Jun 21, 2018 · 3 comments
Assignees

Comments

@jasoncocks
Copy link

When Write-ObjectToSQL when inserting a DateTime, the table is created correctly but subsequent INSERTs rely on DateTime.ToString() ... this has local issues ...

(Get-Date).ToString()
21/06/2018 18:33:17

In order to insert date/time into SQL Server reliably as a nvarchar, Write-ObjectToSQL should convert date/time objects into the SQL Server default date/time format.

E.g.

I have modified the function including ...
$dateformattypes = @{
# PS datatype = SQL data type
'System.DateTime' = 'datetime';
'datetime' = 'datetime';
}

...
if ( $numbertypes.ContainsKey( $datatype ) ){
$null = $strBuilderColumns.Append(", $quoteFirst$prekey$($key.Replace(' ','_'))$quoteLast")

                if ($($InputObject.$key) -ne $null){
                    if ($datatype -eq 'timespan' -or $datatype -eq 'System.TimeSpan') {
                        Write-Verbose "Timespan found ($key). Converting to ticks."
                        $null = $strBuilderValues.Append(", $(($InputObject.$key).Ticks)")
                    }else{
                        $null = $strBuilderValues.Append(", $($InputObject.$key)")
                    }
                    
                }else{
                    $null = $strBuilderValues.Append(", NULL")
                }
            **}elseif ( $dateformattypes.ContainsKey( $datatype ) ){
                $null = $strBuilderColumns.Append(", $quoteFirst$prekey$($key.Replace(' ','_'))$quoteLast")
                $strtmp = $InputObject.$key.ToString("yyyy-MM-dd HH:mm:ss.fff")
                if ($ConnectionString){ 
                    $null = $strBuilderValues.Append(", '$strtmp'")
                }else{
                    $null = $strBuilderValues.Append(", N'$strtmp'")
                }**
            }elseif ( $stringtypes.ContainsKey( $datatype ) ){
                $null = $strBuilderColumns.Append(", $quoteFirst$prekey$($key.Replace(' ','_'))$quoteLast")
                $strtmp = $InputObject.$key -replace "'", "''"
                if ($ConnectionString){ 
                    $null = $strBuilderValues.Append(", '$strtmp'")
                }else{
                    $null = $strBuilderValues.Append(", N'$strtmp'")
                }
@jasoncocks
Copy link
Author

(Get-Date).ToString()
21/06/2018 18:43:11

(Get-Date).ToString("yyyy-MM-dd HH:mm:ss.fff")
2018-06-21 18:43:28.619

@joelwiesmann
Copy link

I did more or less the same local modification by using .toString("o"). Would be really helpful to know that it will find it's way into the final code.

@JohnRoos
Copy link
Owner

This sounds like a great idea. Could you please create a pull request so it can be included in the module?

@JohnRoos JohnRoos self-assigned this Sep 18, 2018
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