I have scoured the internet, and so far, none of the answers provide anything I've been able to use.
I am having serious troubles getting a simple datetime into the SQL Server database with powershell.
The column is of type "datetime" and the language is en-US.
An example of what is in the column value would be
2009-09-16 00:00:00.000
I have attempted to send this same date to the database in powershell, and everything I try has returned a
"Conversion failed when converting date and/or time from character string."
error.
This is my powershell script:
$dateString = "2009-09-16 00:00:00.000"
$id = '12345'
$nd = Get-Date $dateString -Format "yyyy-MM-dd HH:mm:ss:fff"
$query = "Update MyTable Set CreateDate = '@e1' Where ID = '@e2'"
$SqlCmd.Parameters.Clear()
$parm = $SqlCmd.Parameters.Add("@e1", $nd)
$SqlCmd.Parameters.Add("@e2" , $id)
$SqlCmd.CommandText = $query
$SqlCmd.ExecuteNonQuery()
I have tried using [datetime] for the variable, but it always converts whatever string it's looking at into something unlike the format needed above, so it fails.
Everything else, regardless of the fact that it looks right, fails due to the wrong type.
Is there a syntax that will get what is needed to insert/update?
YYYYMMDD hh:mm:ss[.mmm]or theYYYY-MM-DDThh:mm:ss[.nnnnnnn]format defined in ISO 8601. Both formats are not affected by the SET LANGUAGE and SET DATEFORMAT session locale settings. More in Tibor Karaszi's article The ultimate guide to the datetime datatypes.