Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid NULL when using Value-Name Mapping in SQL
    primarykey
    data
    text
    <p>I have a table like the following which is basically used to "give a name" to a value in a table (this table contains values for a bunch of other tables as well, not just for MYTABLE; I've omitted a few irrelevant fields from NAMEVALUEMAP):</p> <pre><code>NAMEVALUEMAP Table --------------------- VALUE_ | NAME_ --------------------- 0 | ZERO 1 | ONE </code></pre> <p>I didn't want to use JOINs so I thought of using Sub-Queries.<br><br> Problem is when a value does not exist in the NAMEVALUEMAP table then NULL is shown.<br> Instead of NULL I want to show the actual value from MYTABLE (MYTABLE has ID field as identity column and contains a few rows):</p> <pre><code>-- //Fine, prints word 'ZERO' when MYTABLE.ABC is 0 SELECT (SELECT NAME_ FROM NAMEVALUEMAP WHERE VALUE_ = (SELECT ABC FROM MYTABLE inner_ WHERE inner_.ID = outer_.ID)) FROM MYTABLE outer_ -- //Not Fine, prints NULL (because "999" is not in NAMEVALUEMAP). In this case, MYTABLE.ABC is 999 -- //Want it to print 999 if the value is not in NAMEVALUEMAP SELECT (SELECT NAME_ FROM NAMEVALUEMAP WHERE VALUE_ = (SELECT ABC FROM MYTABLE inner_ WHERE inner_.ID = outer_.ID)) FROM MYTABLE outer_ -- //Tried COALESCE, but the error is "Invalid column name 'VALUE_'" SELECT COALESCE((SELECT NAME_ FROM NAMEVALUEMAP WHERE VALUE_ = (SELECT ABC FROM MYTABLE inner_ WHERE inner_.ID = outer_.ID)), ABC) FROM MYTABLE outer_ </code></pre> <p><strong>Also</strong>, is there <strong>a better way</strong> to do this sort of value-to-name mapping?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload