Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    5

    Unanswered: Newbie Question - Using Current_Date & Date Calculations

    Hello All,

    I am brand new to DB2 after using SQL Server. What I am confused about is why I can't do the following query in DB2:

    SELECT current_date

    In SQL Server, I can do SELECT GetDate() and it returns a date. However, in DB2 I just get an error.

    Why?

    Also, i'm very, very confused as to how to do date calculations in DB2. I need to take today's date, subtract 1, and then return yesterday's date. Sort of like this:

    SELECT current_date -1

    I can't do this in DB2 because I get an error. To compare, in SQL Server I can do the following:

    SELECT DATEADD(DD, -1, GetDate())

    Why can't I do this in DB2? Or maybe I can?

    Help!

  2. #2
    Join Date
    Apr 2012
    Posts
    1,034
    Provided Answers: 18
    In DB2, use:

    VALUES current date

    or just use that special register when you need it

    Other techniques are available also.

    For date arithmetic, you can just say it in words

    CURRENT DATE - 1 day

    Please read the documentation, it has examples and lots more...

  3. #3
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    I want to recommend this article for you to know about date/time in DB2.
    DB2 Basics: Fun with Dates and Times

    My comment on the article was...
    Covered concisely almost everything need to know(about date/time in DB2) for beginners of DB2
    including those peoples who came from other DBMS.
    I hope this article to be updated by latest DB2 version.
    For example: to include updated built-in functions covering "Custom Date/Time Formatting",

  4. #4
    Join Date
    May 2012
    Posts
    5
    Great, thank you. That worked.

    Quote Originally Posted by db2mor View Post
    Please read the documentation, it has examples and lots more...
    Sorry again for the newbie question, but I went to IBM's DB2 site and there really aren't any good examples there at all regarding dates (that i've found). Or are you referring to different documentation which can be found elsewhere? Can you point me in the right direction?

    Thanks!

  5. #5
    Join Date
    Apr 2012
    Posts
    1,034
    Provided Answers: 18
    When you post, always give your DB2-server version+fixpack, and the operating system name.

    For examples, see
    Datetime operations and durations - IBM DB2 9.7 for Linux, UNIX, and Windows

    But also the item recommended by Tonkuma is great for examples.

  6. #6
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    DB2 follows ANSI standard for most SQL. If SQL Server syntax doesn't work on DB2, you should blame Microsoft.
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  7. #7
    Join Date
    May 2012
    Posts
    5
    Quote Originally Posted by Marcus_A View Post
    DB2 follows ANSI standard for most SQL. If SQL Server syntax doesn't work on DB2, you should blame Microsoft.
    Yeah, well you know, i'm not actually looking to blame anyone, I just want code that works. I fully understand that code between SQL Server and DB2 won't work.

    But, you know, that's why i'm asking questions in here, a DB2 forum?

Posting Permissions

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