Forum Discussion
The given value of type String from the data source cannot be converted to type nvarchar of the spec
Hi,
I am creating a date variable and attempting to write that variable to a SQL table. In my PS script, I do this:
$DateTime = $((Get-Date).ToString('yyyy-MM-dd-hh_mm_ss'))
I have tried setting the column in the SQL table to nvarchar(50), or varchar(50), and I still get this message:
System.InvalidOperationException: The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.
Can someone give me some ideas on what to look for? I am going crazy with this 🙂
Thanks!
- olafhelperBronze Contributor
$DateTime = $((Get-Date).ToString('yyyy-MM-dd-hh_mm_ss'))noodles1030 , your format string is wrong, no culture setting supports underscores in the time portion, see Standard date and time format strings - .NET | Microsoft Learn
- noodles1030Copper Contributor
olafhelper - I have changed that previously and it still will not work. If it is just a string and not datetime, why would that matter?
It is interesting - I can do the same thing with the same PowerShell script and write to a different table and it works. The column in the 2nd table is also nvarchar... any idea on that?
Thanks!
- olafhelperBronze ContributorCan't say, you just posted one line of code, I don't know, what you do with the result in the rest.