Here's a fun one. BOL says that using CONVERT or CAST to change a string into a number won't work.
The following "works" and I'm assuming it is because the compiler can verify that the string value is in fact a number, even with the Unicode N:
declare @col2 int
select @col2 = convert (int, N'12345')
======================================
Here's what I need to work, or I need a creative work-around if someone has an interesting idea:
declare @col2 int,
@myString nvarchar(100)
select @col2 = '12345'
select @myString = convert(int, @col2)
Msg 245, Level 16, State 1, Line 67
Conversion failed when converting the nvarchar value '12345' to data type int.
I'm assuming that since the compiler cannot pre-determine the value of @col2 that it won't allow this.
I understand the responsibility to ensure that the converted string is actually a number. I can put something together as a pre-check for this.
Any suggestions or creative work-arounds would be helpful!
Thanks!
Doug B

Converting @myString to @myInt
jkrakowsky
Looks like you can convert from a string to a number after all, as long as it is a valid number.
Thanks!
DB
Arthur
aspic