Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

datetime and string conversion problem

$
0
0
Hi EveryoneIm pulling some data out of one database with a query that looks like:[quote]$localquery = "SELECT [SessionIdTime],[SessionIdSeq],[InviteTime],[FromUri],[ToUri],[FromUriType],[ToUriType]..........$data = Invoke-Sqlcmd -serverinstance "localhost" -query $localquery[/quote]If I look at the object type of the "SessionIdTime in the $data array it is a date time object and contains the milliseconds that have been pulled from the database.I then put this info into another database (Dont ask :D):[quote]foreach ($d in $data){$qry = "insert into $table VALUES ('$($d.SessionIdTime)', '$($d.SessionIdSeq)', '$($d.InviteTime)', '$FromUri', '$ToUri', '$($d.FromUriType)'.........Invoke-Sqlcmd -Database $database -ServerInstance $server -Query $qry}[/quote]The problem comes in where during this insert the value is put in as a string and the milliseconds are lost which are key to this requirement.I have tried to change the this and do a check as follows:[quote]foreach ($d in $data){$check = $d.SessionIdTime | get-date$qry = "insert into $table VALUES ('$check', '$($d.SessionIdSeq)', '$($d.InviteTime)', '$FromUri', '$ToUri', '$($d.FromUriType)'.........Invoke-Sqlcmd -Database $database -ServerInstance $server -Query $qry}[/quote]I then get the error:Invoke-Sqlcmd : Conversion failed when converting datetime from character string.The value in the array is definitely a date object and there is no other code between this.The field is a datetime field in the destination database.Your assistance with this is much appreciated.

Viewing all articles
Browse latest Browse all 467

Trending Articles