Results 1 to 17 of 17

Thread: [RESOLVED] Date Parameter in OracleParamater

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Resolved [RESOLVED] Date Parameter in OracleParamater

    Hi all,
    I am using this SQL statement to fetch data from oracle

    Code:
    str = "select * from Master where CreatedDttm = TO_DATE(:p_StartDate,'MM/DD/YYYY')"
    and passing the date to oracle parameter as

    Code:
     oParameter.ParameterName = "p_StartDate"
            oParameter.Value = "01/01/2009"
            oParameter.OracleType = OracleType.DateTime
            oParameterList(0) = oParameter
    And executing this command I getting error that

    Invalid varriable name or number ?

    What I am doing wrong here ?

    I did the steps mentioned in post. But the same error
    Please mark you thread resolved using the Thread Tools as shown

  2. #2

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Parameter in OracleParamater

    This also produces the same error...

    Code:
             str = "select * from Master where CreatedDttm = :startDate"
            oParameter.ParameterName = "p_StartDate"
            oParameter.OracleType = OracleType.DateTime
            oParameter.Value = DateTime.ParseExact("01/01/2009", "MM/dd/yyyy", CultureInfo.InvariantCulture)
            oParameterList(0) = oParameter
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Parameter in OracleParamater

    Without DateConversion it is working...

    Code:
    str = "select * from Master where CreatedDttm > :p_StartDate"
    But I need to format it as MM/DD/YYYY hh:mm:ss AM
    Last edited by danasegarane; May 19th, 2010 at 06:34 AM.
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Date Parameter in OracleParamater

    What happens if you substitute @ instead of :

    Code:
    str = "select * from Master where CreatedDttm = TO_DATE(@p_StartDate,'MM/DD/YYYY')"
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Parameter in OracleParamater

    Quote Originally Posted by Pradeep1210 View Post
    What happens if you substitute @ instead of :

    Code:
    str = "select * from Master where CreatedDttm = TO_DATE(@p_StartDate,'MM/DD/YYYY')"
    I am getting the same Error "Illegal Varriable/Number "

    Code:
    str = "select * from vehicleMaster where CreatedDttm =TO_DATE(@p_StartDate,'MM/DD/YYYY')"
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Date Parameter in OracleParamater

    I think you would need to use the conversion in the SELECT list, since you are already passing a date value in the WHERE clause anyways.

    Code:
    str = "select *, TO_DATE(CreatedDttm, 'MM/DD/YYYY') from Master where CreatedDttm = :p_StartDate"
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Parameter in OracleParamater

    I have the User Enter the date in format dd/MM/yyyy and I need to convert, before matching with the table.
    Please mark you thread resolved using the Thread Tools as shown

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Date Parameter in OracleParamater

    Your CreatedDttm is a date column, and you can't alter how it is stored internally. A date column is a date column for the database, irrespective of how it got it. Since you want the output in dd/MM/yyyy format, you would specify it in the SELECT list.
    Since you have already specified that the parameter you are passing is of date type (oParameter.OracleType = OracleType.DateTime), that should be sufficient.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Date Parameter in OracleParamater

    Are you using ODP.NET?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  10. #10
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved Re: Date Parameter in OracleParamater

    Quote Originally Posted by danasegarane View Post
    Without DateConversion it is working...

    Code:
    str = "select * from Master where CreatedDttm > :p_StartDate"
    But I need to format it as MM/DD/YYYY hh:mm:ss AM
    You should be able to use it with the timestamp. I tried something very similar using ODP.NET.

    C# Code:
    1. OracleConnection oConn;
    2.             OracleCommand oCmd;
    3.             OracleParameter oPrm;
    4.  
    5.             string strSQL = "SELECT lease_no, lease_create_date, creation_date, customer_name," +
    6.                                 "lease_status FROM lease_header WHERE "+
    7.                                 "lease_create_date > :p_startdate";
    8.             try
    9.             {
    10.                 oConn = new OracleConnection();
    11.                 oConn.ConnectionString = "Data Source=mydb;User Id=abhijit;Password=xfiles;";
    12.                 oConn.Open();
    13.                 Console.WriteLine(oConn.State);
    14.                 oCmd = new OracleCommand();
    15.                 oCmd.CommandText = strSQL;
    16.                 oCmd.CommandType = System.Data.CommandType.Text;
    17.                 oCmd.Connection = oConn;
    18.                 oPrm = oCmd.CreateParameter();
    19.                 oPrm.ParameterName ="p_startdate";
    20.                 oPrm.OracleDbType = OracleDbType.Date;
    21.                 oPrm.Value = DateTime.Parse("4/9/2010 10:17:17 AM");
    22.                 oCmd.Parameters.Add(oPrm);
    23.                 Console.WriteLine(oCmd.CommandText);
    24.                 using (OracleDataReader reader = oCmd.ExecuteReader())
    25.                 {
    26.                     // Always call Read before accessing data.
    27.                     while (reader.Read())
    28.                     {
    29.                         Console.WriteLine("{0}:{1}:{2}:{3}", reader[0], reader[1], reader[2], reader[3]);
    30.                        
    31.                     }
    32.                 }
    33.  
    34.                 oConn.Dispose();
    35.             }
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  11. #11

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Parameter in OracleParamater

    Quote Originally Posted by abhijit View Post
    You should be able to use it with the timestamp. I tried something very similar using ODP.NET.

    C# Code:
    1. OracleConnection oConn;
    2.             OracleCommand oCmd;
    3.             OracleParameter oPrm;
    4.  
    5.             string strSQL = "SELECT lease_no, lease_create_date, creation_date, customer_name," +
    6.                                 "lease_status FROM lease_header WHERE "+
    7.                                 "lease_create_date > :p_startdate";
    8.             try
    9.             {
    10.                 oConn = new OracleConnection();
    11.                 oConn.ConnectionString = "Data Source=mydb;User Id=abhijit;Password=xfiles;";
    12.                 oConn.Open();
    13.                 Console.WriteLine(oConn.State);
    14.                 oCmd = new OracleCommand();
    15.                 oCmd.CommandText = strSQL;
    16.                 oCmd.CommandType = System.Data.CommandType.Text;
    17.                 oCmd.Connection = oConn;
    18.                 oPrm = oCmd.CreateParameter();
    19.                 oPrm.ParameterName ="p_startdate";
    20.                 oPrm.OracleDbType = OracleDbType.Date;
    21.                 oPrm.Value = DateTime.Parse("4/9/2010 10:17:17 AM");
    22.                 oCmd.Parameters.Add(oPrm);
    23.                 Console.WriteLine(oCmd.CommandText);
    24.                 using (OracleDataReader reader = oCmd.ExecuteReader())
    25.                 {
    26.                     // Always call Read before accessing data.
    27.                     while (reader.Read())
    28.                     {
    29.                         Console.WriteLine("{0}:{1}:{2}:{3}", reader[0], reader[1], reader[2], reader[3]);
    30.                        
    31.                     }
    32.                 }
    33.  
    34.                 oConn.Dispose();
    35.             }


    I tried the TimeStamp Column . But It was not working. I change the type to Varchar. And it worke

    Code:
     '
            str = "select * from vehicleMaster where CreatedDttm > TO_DATE(:p_StartDate,'MM/DD/YYYY HH24:Mi:SS')"
            oParameter.ParameterName = "p_StartDate"
            oParameter.OracleType = OracleType.VarChar
            oParameter.Value = "01/01/2009" 
            oParameterList(0) = oParameter
    Please mark you thread resolved using the Thread Tools as shown

  12. #12
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [RESOLVED] Date Parameter in OracleParamater

    What happens if you change to Date instead of a varchar?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  13. #13

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [RESOLVED] Date Parameter in OracleParamater

    Quote Originally Posted by abhijit View Post
    What happens if you change to Date instead of a varchar?
    I am getting the same Error "Illegal Varriable/Number "
    Please mark you thread resolved using the Thread Tools as shown

  14. #14
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [RESOLVED] Date Parameter in OracleParamater

    Quote Originally Posted by abhijit View Post
    What happens if you change to Date instead of a varchar?
    Quote Originally Posted by danasegarane View Post
    I am getting the same Error "Illegal Varriable/Number "
    You would need to get rid of the TO_DATE function in your Oracle query.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  15. #15

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [RESOLVED] Date Parameter in OracleParamater

    Quote Originally Posted by abhijit View Post
    You would need to get rid of the TO_DATE function in your Oracle query.
    No I cannot . I need to format the data as 24 hr format and check
    Please mark you thread resolved using the Thread Tools as shown

  16. #16
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [RESOLVED] Date Parameter in OracleParamater

    Quote Originally Posted by danasegarane View Post
    No I cannot . I need to format the data as 24 hr format and check
    Agreed. What I am saying is that if you get rid of the TO_DATE and use the parameter type OracleDbDate to assign a date value with a timestamp, it will work.

    That's an alternative solution. I tried that out here and it works correctly.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  17. #17

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [RESOLVED] Date Parameter in OracleParamater

    That I have tried in Post Number #3
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width