title: How to use NVL function as like a if clause, and integrate a subquery?
body: <p>I have this sql query</p>
  <pre><code>SELECT cast(dmh.DOC_NO as varchar(35)) AS DOCUMENT_ID, cast(dmh.DOC_TYPE as varchar(30)) AS DOC_TYPE_ID, dmh.CREATE_DATE as DOC_DATE, dmh.LOCATION as LOCATION, dmh.LOC_TYPE as LOC_TYPE, 'DOC_MERCH' as DOC_SOURCE, 
            (SELECT lcompany_fiscal_num
             FROM (SELECT lcompany_fiscal_num
                   FROM NB_IM_LOC_RIM_START locr, NB_LEGAL_COMPANY legalc
                      WHERE locr.LOC_TYPE = dmh.LOC_TYPE
                          AND locr.LOCATION = dmh.LOCATION
                          AND locr.LOC_START_DATE &lt;= dmh.CREATE_DATE
                          AND legalc.LCOMPANY_CODE = locr.COMPANY
                          ORDER BY locr.LOC_START_DATE desc)
                          WHERE rownum = 1) as COMPANY_ID, 'N' as IS_PRINTED, 'N' as IS_MIGRATED
      FROM NB_DOC_MERCH_HEADER dmh
      WHERE dmh.DOC_TYPE IN ('VFT','NDB','VND','CVFT','VTD','VNC','NCI','NNC','CVNC', 'VFI')
            AND dmh.CREATE_DATE BETWEEN ADD_MONTHS(get_vdate, - (12 * 10)) and get_vdate
            AND dmh.DOCUMENT_ID_SMIS is null;
  </code></pre>
  <p>This query returns me this type of results:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>DOCUMENT_ID</th>
  <th>DOC_TYPE_ID</th>
  <th>DOC_DATE</th>
  <th>LOCATION</th>
  <th>LOC_TYPE</th>
  <th>DOC_SOURCE</th>
  <th>COMPANY_ID</th>
  <th>IS_PRINTED</th>
  <th>IS_MIGRATED</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1600401</td>
  <td>NDB</td>
  <td>11.11.25</td>
  <td>16</td>
  <td>S</td>
  <td>DOC_MERCH</td>
  <td>(null)</td>
  <td>N</td>
  <td>N</td>
  </tr>
  <tr>
  <td>1600401</td>
  <td>NDB</td>
  <td>11.11.25</td>
  <td>160</td>
  <td>W</td>
  <td>DOC_MERCH</td>
  <td>(null)</td>
  <td>N</td>
  <td>N</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>As you can see company_id in some cases are null, and i don't want that to happen</p>
  <p>What i want to do is: when attrib LOC_TYPE = 'S' i want to do one query and when LOC_TYPE = 'W' i want to do another query, in order to solve the null question.</p>
  <p>Is that possible to do with the NVL function? If so, how can i integrate that in my code?</p>