Search code examples
databasesqlitenullcommanddatabase-table

How to display `Null` (SQLite)


On SQLite, I displayed the table "user" as shown below but "Null" is not displayed so I cannot differentiate between "Null" and Blank(Empty String):

sqlite> .header on
sqlite> .mode box
sqlite> select * from user;
┌────┬─────────────────┐
│ id │      name       │
├────┼─────────────────┤
│ 1  │ Steve Jobs      │
│ 2  │                 │ <- Null
│ 3  │                 │ <- Null
│ 4  │ Bill Gates      │
│ 5  │                 │ <- Blank(Empty String)
│ 6  │ Mark Zuckerberg │
└────┴─────────────────┘

Are there any ways to display "Null"?


Solution

  • This command below can set what to show for NULL values:

    .nullvalue <String>
    

    So, set NULL as shown below:

    .nullvalue NULL
    

    Then, NULL is displayed for NULL values as shown below:

    sqlite> .headers on
    sqlite> .mode box
    sqlite> SELECT * FROM person;
    ┌────┬─────────────────┐
    │ id │      name       │
    ├────┼─────────────────┤
    │ 1  │ Steve Jobs      │
    │ 2  │ NULL            │ <- NULL
    │ 3  │ NULL            │ <- NULL
    │ 4  │ Bill Gates      │
    │ 5  │                 │ <- Blank(Empty String)
    │ 6  │ Mark Zuckerberg │
    └────┴─────────────────┘
    

    Next, set This is NULL. as shown below:

    .nullvalue "This is NULL."
    

    Then, This is NULL is displayed for NULL values as shown below:

    sqlite> .headers on
    sqlite> .mode box
    sqlite> SELECT * FROM person;
    ┌────┬─────────────────┐
    │ id │      name       │
    ├────┼─────────────────┤
    │ 1  │ Steve Jobs      │
    │ 2  │ This is NULL.   │ <- NULL
    │ 3  │ This is NULL.   │ <- NULL
    │ 4  │ Bill Gates      │
    │ 5  │                 │ <- Blank(Empty String)
    │ 6  │ Mark Zuckerberg │
    └────┴─────────────────┘
    

    And, these commands below can show the details of the command .nullvalue:

    .help .nullvalue
    

    Or:

    .help nullvalue
    

    Then, this is how it looks like below:

    sqlite> .help .nullvalue  
    .nullvalue STRING        Use STRING in place of NULL values