I need to concatenate into a string used as a select statement that creates a view, and I'm finding it doesn't like the syntax I am using. I already use the same syntax in a case when construct with no problem.
Declare @SQL as varchar(2000)
Set @SQL = 'Select FieldName From...' etc
Declare @MyView as varchar (2000)
Set @MyView = 'alter view ViewName as' +@SQL
Exec (@MyView)
Inside the @SQL variable, this syntax works fine:
CASE WHEN Field_A = 1 then '''+'Yes'+''' else '''+'No'+''' end as FieldAlias
When I try to concatenate into a field (trying to change values such as AA1234567 into AA-123-4567), this syntax doesn't work:
LEFT(Field_B,2)'''+'-'+'''Substring(Field_B,3,3)'''+'-'+'''Right(Field_B,4) as Field_B
I get an error saying it's an invalid SQL statement. What am I doing wrong?
Declare @SQL as varchar(2000)
Set @SQL = 'Select FieldName From...' etc
Declare @MyView as varchar (2000)
Set @MyView = 'alter view ViewName as' +@SQL
Exec (@MyView)
Inside the @SQL variable, this syntax works fine:
CASE WHEN Field_A = 1 then '''+'Yes'+''' else '''+'No'+''' end as FieldAlias
When I try to concatenate into a field (trying to change values such as AA1234567 into AA-123-4567), this syntax doesn't work:
LEFT(Field_B,2)'''+'-'+'''Substring(Field_B,3,3)'''+'-'+'''Right(Field_B,4) as Field_B
I get an error saying it's an invalid SQL statement. What am I doing wrong?