Skip to content
Advertisement

How to use a SQL keyword NOT as a keyword

I am trying to transform a table of data–I want the rows to become the columns, and the columns to become the rows (like a total complete pivot). I am using the method from this answer to do so.

If it makes any difference, I am running my own SQL server on a Raspberry Pi using PHPMyAdmin.

The table (truncated) looks like this:

I’m trying to transpose it into this:

And my query is here:

In part of the code I need to write WHEN ‘Grant’ THEN Grant but that second unquoted Grant becomes a keyword and messes with my code. When I put Grant in single quotes it throws me a syntax error further down where I write ‘) c’ but I suspect my little cheat probably has something to do with it. What shall I do?

Advertisement

Answer

You seem to be suggesting that Grant is a column name — bad choice, but you are stuck with it. The standard way to escape names is to use double quotes:

Some databases use backticks or square braces instead:

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