Search code examples
sqlsybasesap-aseisnull

Selecting with quotes with ISNULL


I am trying to build a dynamic query in sybase like:

SELECT @v_query = @v_query + " tech= "+ ISNULL(''''+@stat+'''',"tech")

When I pass @stat as NULL, I get '' as output but expected output is tech which is wrong

If I pass@stat as NACK, it should return 'NACK' which works as expected

Thanks for any suggestions on how to achieve this


Solution

  • To be honest, you probably want this:

    SELECT @v_query = @v_query + 
                      (CASE WHEN @stat IS NULL THEN '1=1'
                            ELSE 'tech = ''' + @stat + ''''
                       END)
    

    Don't use tech = tech as a no-op (it doesn't work for NULLs). Instead, just remove the comparison.