Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

NVL in TO CHAR

I have my query as below

/* Formatted on 26/04/2011 4:44:25 PM (QP5 v5.136.908.31019) */
SELECT TRANSACTIONMONTH
  FROM MONTHLYBILL
 WHERE TO_CHAR (TO_DATE (CREATEDDATE), 'MONTH-YYYY') =
          TO_CHAR (TO_DATE ('1/02/2011', 'DD/MM/YYYY'), 'MONTH-YYYY')
       AND EMPLOYEE_ID = 4

When TO_CHAR (TO_DATE ('1/02/2011', 'DD/MM/YYYY'), 'MONTH-YYYY') is passed NULL
I want to return the first available row in DB based on MIN(Createddate) and employee_id=4

IN addition, the ouput of above query is "FEBRUARY-2011"
I need one more column in output which returns first date of month following TRANSACTIONMONTH returned
0
GRChandrashekar
Asked:
GRChandrashekar
  • 16
  • 11
1 Solution
 
sdstuberCommented:
First....

>>> TO_DATE (CREATEDDATE)

this is wrong,   if createdate is already a date, then don't apply to_date to it
if createdate is a string, then you should have an explicit mask to convert it with the appropriate format

assuming it's a date then try this...


 WHERE     createdate >= TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm')
       AND createdate < ADD_MONTHS(TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm'), 1)
       AND employee_id = 4;

it's a little bit more code but is much more efficient

if it's a string, then do the same thing except wrap createdate with to_date()  including the appropriate date mask




0
 
sdstuberCommented:
for the actual NVL part

use the MIN analytic

nvl(transactionmonth,min(transactionmonth) over())
0
 
GRChandrashekarAuthor Commented:
Can you please alter this query.

WHERE     createdate >= TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm')
       AND createdate < ADD_MONTHS(TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm'), 1)
       AND employee_id = 4;

And what about  one more column in output which returns first date of month following TRANSACTIONMONTH returned
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
sdstuberCommented:
first date of month following transaction month is


ADD_MONTHS(TRUNC(transactionmonth,'mm'), 1)
0
 
sdstuberCommented:
>>> Can you please alter this query.

what do you want to alter about it?
it's not a full query, that's just a suggested where clause
0
 
GRChandrashekarAuthor Commented:
I want this in output column
ADD_MONTHS(TRUNC(transactionmonth,'mm'), 1)
0
 
sdstuberCommented:
>>> use the MIN analytic

actually this won't work, because the analytic will only return the min date within the where clause date range.

If you want the min date for the whole table, for that employee then you'll have to use a subquery.

I suggest using case or decode  instead of NVL because NVL always evaluates both fields, which you don't really want to do if the data isn't null

something like this...
(case when transactionmonth is null then (select min(transactionmonth) from monthlybill where employee_id = 4) else transactionmonth end)
0
 
sdstuberCommented:
http:#35466369

ok, use that function call exactly as shown as a column
0
 
sdstuberCommented:
if you want the generated transactionmonth (the one with the NULL handling) to be used in the next month call  then try something like this...


SELECT transactionmonth, ADD_MONTHS(TRUNC(transactionmonth, 'mm'), 1) nextmonth
  FROM (SELECT (CASE
                    WHEN transactionmonth IS NULL
                    THEN
                        (SELECT MIN(transactionmonth)
                           FROM monthlybill
                          WHERE employee_id = 4)
                    ELSE
                        transactionmonth
                END)
                   transactionmonth
          FROM monthlybill
         WHERE     createdate >= TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm')
               AND createdate < ADD_MONTHS(TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm'), 1)
               AND employee_id = 4);

Open in new window

0
 
GRChandrashekarAuthor Commented:
SQL command not properly ended
0
 
sdstuberCommented:
the query above assumes createdate is, in fact, a date

if not, then modify as described previously
0
 
sdstuberCommented:
>>> SQL command not properly ended

what query did you run?  It works fine for me when I tested it
0
 
GRChandrashekarAuthor Commented:
Error SQL command not properly ended
0
 
sdstuberCommented:
>>> Error SQL command not properly ended

what did you run? again, I tested what I posted, it works correctly for me
0
 
GRChandrashekarAuthor Commented:
It was this one what u posted ID: 35466416
0
 
sdstuberCommented:
are you using Toad?  If so, do you have other queries in your editor window that may or may not have semicolons ?
0
 
GRChandrashekarAuthor Commented:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER

TRUNC(transactionmonth, 'mm'), 1) nextmonth


transactionmonth THIS FIELD STORES DATA LIKE FEBRUARY-2011, MARCH-2011 ETC
0
 
sdstuberCommented:
then you'll need to covert it to a date

TO_DATE(transactionmonth,'fmMONTH-yyyy')
0
 
sdstuberCommented:
the "fm" isn't really necessary
0
 
GRChandrashekarAuthor Commented:
Perfect.  Now only problem is passing NVL which returns min and the first date following the ouput returned by min
0
 
sdstuberCommented:
that's already done in the query posted above

if not,  please post your final version of the query you are using

along with sample data and expected results
0
 
GRChandrashekarAuthor Commented:
SELECT transactionmonth, ADD_MONTHS(TO_DATE(transactionmonth,'fmMONTH-yyyy'), 1) nextmonth

  FROM (SELECT (CASE
                    WHEN transactionmonth IS NULL
                    THEN
                        (SELECT MIN(transactionmonth)
                           FROM monthlybill
                          WHERE employee_id = 4)
                    ELSE
                        transactionmonth
                END)
                   transactionmonth
          FROM monthlybill
         WHERE     CREATEDDATE >= TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm')
               AND CREATEDDATE < ADD_MONTHS(TRUNC(TO_DATE('1/02/2011', 'DD/MM/YYYY'), 'mm'), 1)
               AND employee_id = 4);

Now if i pass null dates in the last 2 lines, it wont return any record at all
0
 
GRChandrashekarAuthor Commented:
expected result is when dates are null, it should return first available transactionmonth, ADD_MONTHS(TO_DATE(transactionmonth,'fmMONTH-yyyy'), 1) nextmonth
in DB for employee_ID based on MIN (createddate) in DB for that employeeID
0
 
sdstuberCommented:
I meant "show me" expected results given some sample data

but...maybe I've misunderstood all along.

when you say null dates,  do you mean null in the data you are using as the where clause filters or do you mean null in the data in the table?
0
 
GRChandrashekarAuthor Commented:
let us say in the where clause  i pass createddate as null

now let us say in DB the min(Createddated) is 4/1/2011, then output will be
January-2011 which is transaction month in DB (if present in DB else obvious output will be null)
and nextmonth will be 1/2/2011

0
 
sdstuberCommented:
yes, definitely SHOW, don't describe.

but, I "think" this is what you just said.

If not,  please SHOW me.  That is show me data, and show me results as data.
Remove all ambiguity of words and misunderstandings.
If this isn't correct, post data and I can replicate and results and I can test for.


SELECT transactionmonth, ADD_MONTHS(TO_DATE(transactionmonth, 'MONTH-yyyy'), 1) nextmonth
  FROM monthlybill
 WHERE     createdate >= NVL(TRUNC(TO_DATE(NULL, 'DD/MM/YYYY'), 'mm'),
                             (SELECT MIN(TO_DATE(transactionmonth, 'MONTH-yyyy'))
                                FROM monthlybill
                               WHERE employee_id = 4)
                            )
       AND createdate < ADD_MONTHS(NVL(TRUNC(TO_DATE(NULL, 'DD/MM/YYYY'), 'mm'),
                                       (SELECT MIN(TO_DATE(transactionmonth, 'MONTH-yyyy'))
                                          FROM monthlybill
                                         WHERE employee_id = 4)
                                      ),
                                   1
                                  )
       AND employee_id = 4;
0
 
GRChandrashekarAuthor Commented:
Genius
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 16
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now