Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Invalid argument in a DateDiff() function

Invalid argument in a DateDiff() function

Scheduled Pinned Locked Moved Database
helpdatabase
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheJudeDude
    wrote on last edited by
    #1

    I am getting an error when I run the following query:

    "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DateDiff('d',2009-11-08,[TICKET-NEXT-DUE-DATE]) AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')"

    The error is "Invalid argument to Function. Function name is DateDiff" The 'd' is a standard argument, The date is standard, and so is the data field [TICKET-NEXT-DUE-DATE]. Here is how I have it in the code:

    SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, " +
    "DateDiff('d'," + strDate + ",[TICKET-NEXT-DUE-DATE]) AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')

    This statement I took straight out of MS Access. Any help would be appreciated!

    Jude

    L 1 Reply Last reply
    0
    • T TheJudeDude

      I am getting an error when I run the following query:

      "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DateDiff('d',2009-11-08,[TICKET-NEXT-DUE-DATE]) AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')"

      The error is "Invalid argument to Function. Function name is DateDiff" The 'd' is a standard argument, The date is standard, and so is the data field [TICKET-NEXT-DUE-DATE]. Here is how I have it in the code:

      SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, " +
      "DateDiff('d'," + strDate + ",[TICKET-NEXT-DUE-DATE]) AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')

      This statement I took straight out of MS Access. Any help would be appreciated!

      Jude

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      within SQL statements literal dates need delimiters, either ' or # or [] depending on DB. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        within SQL statements literal dates need delimiters, either ' or # or [] depending on DB. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        T Offline
        T Offline
        TheJudeDude
        wrote on last edited by
        #3

        Thanx Luc! That brought me to another error of Invalid Function Name. Function name is DATEDIFF??? Isn't datediff() a standard function? As I said before, the same string works in Access.... Here's what the string looks like now: "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DATEDIFF('d','2009-11-08','2009-11-08') AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')" Thanx!

        Jude

        T 1 Reply Last reply
        0
        • T TheJudeDude

          Thanx Luc! That brought me to another error of Invalid Function Name. Function name is DATEDIFF??? Isn't datediff() a standard function? As I said before, the same string works in Access.... Here's what the string looks like now: "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DATEDIFF('d','2009-11-08','2009-11-08') AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')" Thanx!

          Jude

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #4

          TheJudeDude wrote:

          DATEDIFF('d','2009-11-08','2009-11-08')

          You now have to many quotes. It should be one of the following:

          DATEDIFF(d,'2009-11-08','2009-11-08')

          Or

          DATEDIFF(dd,'2009-11-08','2009-11-08')

          Or

          DATEDIFF(day,'2009-11-08','2009-11-08')

          Of course this is a poor example as datediff of the same dates is always 0.

          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

          T 1 Reply Last reply
          0
          • T The Man from U N C L E

            TheJudeDude wrote:

            DATEDIFF('d','2009-11-08','2009-11-08')

            You now have to many quotes. It should be one of the following:

            DATEDIFF(d,'2009-11-08','2009-11-08')

            Or

            DATEDIFF(dd,'2009-11-08','2009-11-08')

            Or

            DATEDIFF(day,'2009-11-08','2009-11-08')

            Of course this is a poor example as datediff of the same dates is always 0.

            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

            T Offline
            T Offline
            TheJudeDude
            wrote on last edited by
            #5

            True, bad example..I was just trying to troubleshoot, so I just pasted in the same date. I saw the bad quotes and changed that: "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DATEDIFF(d,'2009-11-08','2009-11-08') AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')" But I am still getting an error stating:Build Error : Invalid Function Name, Function name is 'DATEDIFF'. ??

            Jude

            L 1 Reply Last reply
            0
            • T TheJudeDude

              True, bad example..I was just trying to troubleshoot, so I just pasted in the same date. I saw the bad quotes and changed that: "SELECT TICKET-STORE, TICKET-ACCT-MANAGER, TICKET-TYPE, TICKET-CONTRACT-DATE, TICKET-NEXT-DUE-DATE, TICKET-LAST-PAID-DATE, DATEDIFF(d,'2009-11-08','2009-11-08') AS Expr1, TICKET-TICKET-NBR FROM TICKET WHERE (TICKET-TYPE ='O')" But I am still getting an error stating:Build Error : Invalid Function Name, Function name is 'DATEDIFF'. ??

              Jude

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              DateDiff is an SQL function, how can a C#, C++, or VB compiler complain about it? FWIW: If your SQL statement spans more than a single line of source code, you should take appropriate measures, which depend on your programming language. Please show actual code and error message. And check the line numbers as to where the error occurs. :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              T 1 Reply Last reply
              0
              • L Luc Pattyn

                DateDiff is an SQL function, how can a C#, C++, or VB compiler complain about it? FWIW: If your SQL statement spans more than a single line of source code, you should take appropriate measures, which depend on your programming language. Please show actual code and error message. And check the line numbers as to where the error occurs. :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                T Offline
                T Offline
                TheJudeDude
                wrote on last edited by
                #7

                I shortened up the SQL a bit, but I am getting the same error The error is on DbReader = DbCommand.ExecuteReader():

                	OdbcConnection DbConnection = new OdbcConnection("DSN=rsss");
                		OdbcCommand DbCommand = DbConnection.CreateCommand();
                		DbCommand.CommandText = "SELECT TICKET-STORE, DATEDIFF(day,'" + strDate + "','" + strDate + "') AS EXP FROM TICKET";
                		DbConnection.Open();
                		try
                		{
                			DbReader = DbCommand.ExecuteReader();
                			while(DbReader.Read())
                			{
                				intStore = Int32.Parse(DbReader\["TICKET-STORE"\].ToString());
                			}
                		}
                
                		catch(OdbcException caught)
                		{
                			Console.WriteLine(caught.ToString());
                			Console.Read();
                			
                			DbConnection.Close();
                		}
                

                Here is the error:

                System.Data.Odbc.OdbcException: ERROR [HY000] Build Error: Invalid Function Name. Function name is 'DATEDIFF'.
                at System.Data.Odbc.OdbcConnection.HandleError(HandleRef hrHandle, SQL_HANDLE hType, RETCODE retcode)
                at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method)
                at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
                at System.Data.Odbc.OdbcCommand.ExecuteReader()
                at acctmgr.Class1.Main(String[] args) in c:\documents and settings\brian\my documents\visual studio projects\acctmgr\class1.cs:line 43

                Thanx for your help!

                Jude

                L 1 Reply Last reply
                0
                • T TheJudeDude

                  I shortened up the SQL a bit, but I am getting the same error The error is on DbReader = DbCommand.ExecuteReader():

                  	OdbcConnection DbConnection = new OdbcConnection("DSN=rsss");
                  		OdbcCommand DbCommand = DbConnection.CreateCommand();
                  		DbCommand.CommandText = "SELECT TICKET-STORE, DATEDIFF(day,'" + strDate + "','" + strDate + "') AS EXP FROM TICKET";
                  		DbConnection.Open();
                  		try
                  		{
                  			DbReader = DbCommand.ExecuteReader();
                  			while(DbReader.Read())
                  			{
                  				intStore = Int32.Parse(DbReader\["TICKET-STORE"\].ToString());
                  			}
                  		}
                  
                  		catch(OdbcException caught)
                  		{
                  			Console.WriteLine(caught.ToString());
                  			Console.Read();
                  			
                  			DbConnection.Close();
                  		}
                  

                  Here is the error:

                  System.Data.Odbc.OdbcException: ERROR [HY000] Build Error: Invalid Function Name. Function name is 'DATEDIFF'.
                  at System.Data.Odbc.OdbcConnection.HandleError(HandleRef hrHandle, SQL_HANDLE hType, RETCODE retcode)
                  at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method)
                  at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
                  at System.Data.Odbc.OdbcCommand.ExecuteReader()
                  at acctmgr.Class1.Main(String[] args) in c:\documents and settings\brian\my documents\visual studio projects\acctmgr\class1.cs:line 43

                  Thanx for your help!

                  Jude

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I don't see anything wrong, but then I'm not a DB expert at all. However, from earlier messages, I think you target SQL Server 2000, and this[^] seems to suggest you use SqlConnection instead of OdbcConnection. Which would mean a different connection string, some C# code changes, and probably some SQL changes too. :)

                  Luc Pattyn


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  T 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I don't see anything wrong, but then I'm not a DB expert at all. However, from earlier messages, I think you target SQL Server 2000, and this[^] seems to suggest you use SqlConnection instead of OdbcConnection. Which would mean a different connection string, some C# code changes, and probably some SQL changes too. :)

                    Luc Pattyn


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    T Offline
                    T Offline
                    TheJudeDude
                    wrote on last edited by
                    #9

                    I figured it out. The contacted the company who produced the connector. It does not support a DateDiff() function :-/ I also contacted the third party vender and they stated that they have a built in function for it. Hopefully that works. Thanks a lot for your time and help!

                    Jude

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups