The given value of type String from the data source cannot be converted to type nvarchar of the spec

Copper Contributor

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!

 

4 Replies

 


$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

@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!

Can't say, you just posted one line of code, I don't know, what you do with the result in the rest.

@olafhelper 

Thanks for your help - I just ended up using nvarchar to solve this. However, I think it could have been a different column having the issue than the one I was debugging. I never found a way to determine which other date columns might have been the problem. Please let me know if you know of a way to debug a bulk copy ...

Thanks very much.