Solved

Trouble with Date concatenation with varchar2 string in oracle

Posted on 2008-10-26
2
2,219 Views
Last Modified: 2013-12-07
Hi
I'm trying to concatenate dates with other Sql statements so I can place common Where statements in 1 procedure.

I am running into troubles with the date.
What is happening is To and From dates are in format dd-mm-yyyy as in 10-01-2008

When I concatenate into v_SQL which is varchar2 it becomes dd-MONth- YY as in 10-JAN-08

Can somebody please explain why and how to fix?
-- v_inputdate is varchar2
 

v_SQL varchar2(1000);

 v_strTmp varchar2(100);

 v_WhereSet number;

 v_ToDate DATE;

 v_FromDate DATE; 

 

 BEGIN

 

 -- Input Date

 

  If Length(v_InputDate) > 0 Then
 

  -- Date that is input is 10th January 2008
 

	v_FromDate := To_Date(v_InputDate,'dd-mm-yyyy');

-- I am now 10-01-2008	
 

If v_ReportType = 'D' Then

		v_ToDate := v_FromDate + 1 ;

-- I am now 11-01-2008
 

	 End If; 

	 

	 If v_WhereSet = 1 Then

		v_strTmp := ' AND ';

	 Else

		v_WhereSet := 1;

		v_strTmp := ' WHERE  ';		

	 End If;

	 v_SQL := v_SQL || v_strTmp || ' pr.pres_dt >= ' || v_FromDate || 

				 ' AND pr.pres_dt < '  || v_ToDate;   

-- At this point v_SQL changes to have v_FromDate as 10-JAN-08  

-- My data is stored as dd-mm-yyyy.
 

  End If;

Open in new window

0
Comment
Question by:cmrnp
2 Comments
 
LVL 10

Accepted Solution

by:
dbmullen earned 500 total points
ID: 22810072
well..   you're really making things hard on your self...

hard way:
         v_SQL := v_SQL || v_strTmp || ' pr.pres_dt >= to_date(''' || to_char(v_FromDate,'yyyymmdd') ||
                                ''',''yyyymmdd'')  AND pr.pres_dt < to_date('''  || to_char(v_ToDate,'yyyymmdd') || ''',''yyyymmdd'')';

easy way:
         v_sql := 'select count(*) from use_binds pr where pr.pres_dt >= :v_from_date and pr.pres_dt < :v_todate';        
         execute immediate v_sql into yadda using v_fromdate, v_todate;



create table use_binds as select sysdate pres_dt from dual;
 

set serveroutput on

declare 

v_sql varchar2(3000);

v_strtmp varchar2(100) := 'table';

v_fromdate date := trunc(sysdate);

v_todate   date := trunc(sysdate) + 1;

yadda number(10) := 0;
 

begin

         v_SQL := v_SQL || v_strTmp || ' pr.pres_dt >= to_date(''' || to_char(v_FromDate,'yyyymmdd') || 

                                ''',''yyyymmdd'')  AND pr.pres_dt < to_date('''  || to_char(v_ToDate,'yyyymmdd') || ''',''yyyymmdd'')';

                                

         dbms_output.put_line(v_sql);

         

         v_sql := ' ' ;

         v_sql := 'select count(*) from use_binds pr where pr.pres_dt >= :v_from_date and pr.pres_dt < :v_todate';

         dbms_output.put_line(v_sql);

         

         execute immediate v_sql into yadda using v_fromdate, v_todate;

         dbms_output.put_line(to_char(yadda) );

         

end;                                

Open in new window

0
 
LVL 1

Author Closing Comment

by:cmrnp
ID: 31510189
Brilliant

Most questions i've had have not been answered but this was spot on.

Thanks heaps.
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup

896 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

15 Experts available now in Live!

Get 1:1 Help Now