PowerShell date format not accepted in SQL

蹲街弑〆低调 提交于 2021-02-05 06:37:11

问题


I'm having kind of a strange problem. When the server's Region and Language settings is set to English (United States) there are no issues with objects containing the date and time. But when I change it to my countries local Dutch (Belgium) I am experiencing problems in some of my PowerShell scripts.

Is there somewhere a variable in PowerShell that needs to be set to fix this? Or do I have to default to English date formats on the server all the time?

We use for example the SQL module provided by Don Jones for filling up an SQL database. This line generates an error when it's set to Dutch, but not when it's set to English:

if ($properties[$property] -eq 'datetime2') { [datetime]$prop = $result.GetDateTime($result.GetOrdinal($property)) }

The only thing I do to retrieve the date is Get-Date without anything special. And it generates this error when set to Dutch (Belgium):

Exception calling "ExecuteNonQuery" with "0" argument(s): "Conversion failed when converting date and/or time from char
acter string."

I've experienced the same problem when generating stuff for Excel sheets. It also complains about the date time format.


回答1:


For exporting information like DateTime fields to SQL, the default language settings English (United States) are used.

So when your Region and Language settings are different from the default for SQL, being: English (United States) <> Dutch (Belgium), you should use the following format in your code to comply to the English defaults:

"Key" = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
"Key" = $_.whenCreated.ToString("yyyy-MM-dd HH:mm:ss")

Another solution is to change the default language for input in SQL Express 2014:

  1. Right click the server instance and select Properties > Advanced
  2. Change Default language to Dutch (or what you use)
  3. Confirm with Ok
  4. Still in SQL Management Studio go to Security > Logins (below Databases)
  5. Double click your user name and select Dutch below as Default language
  6. Restart SQL Management Studio and you should be good to go

Result: When you use the CmdLet Get-Date in your PowerShell code it will work as designed when transferring data to SQL. The system language for SQL and Windows is still English but the formatting used is Dutch.




回答2:


I use this for Datetime2 in SQL Server.

Function Get-Datetime2([datetime]$Date = $(Get-Date) ) {
    $DT = Get-Date -Date $Date
    [string]$DateTime2 =  $DT.Month.ToString() + '/'
    [string]$DateTime2 += $DT.Day.ToString() + '/'
    [string]$DateTime2 += $DT.Year.ToString() + ' '
    [string]$DateTime2 += $DT.TimeOfDay.ToString()
    return $DateTime2
}

Get-Datetime2

returns something that looks like this.

3/12/2018 03:04:34.7496659



回答3:


If you already use Don Jones' module you may already be familiar with type-extension through the *.types.ps1xml... There should be one in his module too, if I remember correctly.

When adding the following type declaration,

  <Type>
    <Name>System.DateTime</Name>
    <Members>
      <ScriptMethod>
        <Name>ToString</Name>
        <Script>
          $(Get-Date $This -Format "yyyy-MM-dd HH:mm:ss")
        </Script>
      </ScriptMethod>
    </Members>
  </Type>

You basically override System.DateTime's ToString() method to the format needed by a standard SQL Server installation.

This way you can make sure that every time you load the SQL module dates they are being formatted the right way.



来源:https://stackoverflow.com/questions/25055172/powershell-date-format-not-accepted-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!