0

Is there a way to return a blank string for a field in SQL, even if the field has data?

SELECT NAME, AGE, DOB, GENDER, CITY
FROM TABLE

I want DOB, to always return blank ''

Agent_Sully
  • 255
  • 1
  • 2
  • 10
  • we can put the literal string in the SELECT list and give it a column alias, e.g. `SELECT NAME, AGE, ' ' AS DOB, GENDER, CITY FROM TABLE`. (If we don't need to return the value of the DOB column, then we don't reference the column in the SELECT list.) To return a blank, we need to return a space character `' '`; Oracle interprets a zero length string literal `''` as a NULL value. – spencer7593 Sep 15 '17 at 14:43

3 Answers3

2

In Oracle, an empty VARCHAR2 is equivalent to a NULL value.

So you can just do:

SELECT NAME,
       AGE,
       NULL AS DOB,
       GENDER,
       CITY
FROM   TABLE

If you want to give it a specific data type then you can use CAST:

SELECT NAME,
       AGE,
       CAST( NULL AS DATE ) AS DOB,
       GENDER,
       CITY
FROM   TABLE
MT0
  • 143,790
  • 11
  • 59
  • 117
1

try this:

SELECT NAME, AGE, ' ' as DOB, GENDER, CITY
FROM TABLE
BWS
  • 3,786
  • 18
  • 25
0

You could use NVL2:

SELECT NVL2(dob, '', NULL) --rest of cols
FROM TABLE;

Please note that '' is the same as NULL. So you have:

SELECT '' AS DOB -- rest of cols
FROM TABLE;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • `NVL2(dob, '', NULL)` is the same as `NVL2(dob, NULL, NULL)` which will always give the output `NULL` - so using `NVL2` is just over-complication. – MT0 Sep 15 '17 at 14:55
  • @MT0 Yup, that is why there is simple second part. I just wanted to show how to easily compare with `NULL` without using `CASE`. – Lukasz Szozda Sep 15 '17 at 14:55