LONG

LONG is a datatype in SQL that's used to store variable length character strings. It can store up to two gigabytes of character data in a database. However, LONG columns have many restrictions and are used less frequently compared to LOBs (Large Objects). Oracle recommends using LOB or VARCHAR2 types instead of LONG for new development work.

Example

CREATE TABLE Example (
ID NUMBER,
LongColumn LONG
);
INSERT INTO Example (ID, LongColumn) VALUES (1, 'Long text data type example.');
SELECT * FROM Example;

Output

| ID | LongColumn |
| --- | ---------------------------- |
| 1 | Long text data type example |

Explanation

In the above example, a table ‘Example’ is created with ID as NUMBER and LongColumn as LONG. It then inserts a row with the ID as 1, and LongColumn as ‘Long text data type example.’. Lastly, it fetches the data from the table. The returned result is the data where ID is 1, and LongColumn is ‘Long text data type example.’.

Example

DECLARE @LongExample AS VARCHAR(MAX);
SET @LongExample = REPLICATE('A', 8001);
SELECT LEN(@LongExample) AS 'LongStringLength';

Output

LongStringLength
-----------------
8001

Explanation

This example illustrates the use of the data type VARCHAR(MAX), which allows for storage of variable-length non-Unicode character data up to 2 gigabytes of text data. It declares a variable @LongExample as VARCHAR(MAX) and sets it to a string of 8001 characters long. The SELECT statement is then used to return the length of the string, which is 8001.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.