Skip to content
Advertisement

Replace `0` with `N/A` or `-1` in pivot query result

I have Attendance table in which date and attendance is stored and I am passing date range in this query to display attendance report.

Now my question is how can I replace 0 (which I am getting as a output if the date passsed doesn’t match with the date inside the Attendance table) with N/A or -1 ?

Advertisement

Answer

You could use CASE as Brandon Miller suggested. Here’s another option – you can use NULLIF to replace a zero with a null value, and then replace any null value with N/A. You’ll need to create a 2nd variable to represent your columns in the select statement of your dynamic query. Here’s a full example with test data:

Outputs:

For fun, here’s a function you can use to convert the @cols variable to the @cols_select variable:

So now you can just call the function when you’re building the query, like this:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement