Solved

NVL in TO CHAR

Posted on 2011-04-26
27
520 Views
Last Modified: 2012-06-27
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
Comment
Question by:GRChandrashekar
  • 16
  • 11
27 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
for the actual NVL part

use the MIN analytic

nvl(transactionmonth,min(transactionmonth) over())
0
 

Author Comment

by:GRChandrashekar
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
first date of month following transaction month is


ADD_MONTHS(TRUNC(transactionmonth,'mm'), 1)
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
>>> 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
 

Author Comment

by:GRChandrashekar
Comment Utility
I want this in output column
ADD_MONTHS(TRUNC(transactionmonth,'mm'), 1)
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
>>> 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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
http:#35466369

ok, use that function call exactly as shown as a column
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
 

Author Comment

by:GRChandrashekar
Comment Utility
SQL command not properly ended
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
the query above assumes createdate is, in fact, a date

if not, then modify as described previously
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
>>> SQL command not properly ended

what query did you run?  It works fine for me when I tested it
0
 

Author Comment

by:GRChandrashekar
Comment Utility
Error SQL command not properly ended
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
>>> Error SQL command not properly ended

what did you run? again, I tested what I posted, it works correctly for me
0
 

Author Comment

by:GRChandrashekar
Comment Utility
It was this one what u posted ID: 35466416
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
are you using Toad?  If so, do you have other queries in your editor window that may or may not have semicolons ?
0
 

Author Comment

by:GRChandrashekar
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
then you'll need to covert it to a date

TO_DATE(transactionmonth,'fmMONTH-yyyy')
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
the "fm" isn't really necessary
0
 

Author Comment

by:GRChandrashekar
Comment Utility
Perfect.  Now only problem is passing NVL which returns min and the first date following the ouput returned by min
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
 

Author Comment

by:GRChandrashekar
Comment Utility
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
 

Author Comment

by:GRChandrashekar
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
 

Author Comment

by:GRChandrashekar
Comment Utility
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
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:GRChandrashekar
Comment Utility
Genius
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now