Search code examples
sql-servert-sqlcoalesce

Issue with coalesce in T-SQL


Declare @Query Var-char(100)

select @Query= 'select coalesce(NULL,"test") as testing' 

Exec @Query

error is

The name 'select coalesce(NULL,"test") as testing' is not valid identifier.

how to fix this?


Solution

  • 1) escape the ' within the query string by typing it twice (don't use the double quotes as in your example) 2) put () after the exec command

    Declare @Query Varchar(100)
    select @Query='select coalesce(NULL,''test'') as testing'
    Exec (@Query)