Skip to content
Advertisement

SQL/Regex Challenge/Puzzle: How to remove comments from SQL code (by using SQL query)?

Requirements

  • Single-lines comments (e.g. — my comment) should be removed.
  • Multi-line comments (e.g. /* my comment */) should be removed.
  • The content of strings literals (e.g. ‘this is a multi-line comment: /* my comment */’) should be ignored.
  • The content of identifiers (e.g. “– column 1 –“) should be ignored.

literals and identifiers

Literals and identifiers can span over multiple lines

Single-line comments

A single-line comment might be the last element of the code and might not end with a newline.

Nested multi-line comments

In databases such as SQL Server and PostgreSQL, multi-line comments can be nested, e.g –

The following code is invalid since only the inner comment is closed:

In databases such as Teradata, Oracle, MySql and SQLite there is no concept of nested comments. The following code is invalid since the comment is already closed with the leftmost */.

This however is a valid code:

Advertisement

Answer

Solutions

Teradata

Oracle

Result

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