Solved

Formatting a timestamp field on the select statement in DB2?

Posted on 2004-08-30
3
446 Views
Last Modified: 2006-11-17
How do I format a timestamp field during a select statement?

For instance, I am selecting this:

     select my_timestamp, substr(my_text, 1, 80), substr(my_code, 1, 1) from my_table where my_timestamp > '2004-08-09-12.00.00'

but the output starts like this:

     2004-08-09-12.00.00.25342 my text starts here and goes for a bunch o chars  . . .   A

What I would like is this:

     2004-08-09-12:00.00 my text starts here and goes for a bunch o chars  . . .  A

Or better yet, I'd like to strip out the date (not the time) and automagically feed in the current date in the where clause... Right now, I just type in the date in my sql statement...

-- Bubba
0
Comment
Question by:bganoush
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
knel1234 earned 250 total points
ID: 11942318
Hi,

Please run this select in a SQL window (or session) and let me know if it provides the desired results for the format.  This will select the current data for the local system's local time.  If you want to format a specific field in a table then just use the to_char(<fieldname>,'YYYY-MM-DD')
I did not include the hours and minutes but that it easy enough to do (HH:MM if you are interested)
Please let me know if this helps or if you have any additional questions.

select to_char(sysdate,'YYYY-MM-DD') from dual;


thanks
knel
0
 

Author Comment

by:bganoush
ID: 11943136

That doesn't work but I managed to find a good DB2 reference about dates and times and this works:

    select time(my_time_field) from my_table where my_time_field > '2004-08-31-08.00.00'

... now all I need to do is get the current date and time...

That would be with:

    select current date FROM sysibm.sysdummy1
    select current time FROM sysibm.sysdummy1
    select current timestamp FROM sysibm.sysdummy1

so now, how do I use that to make sure I only return the "my_time_field" where it is dated "today"?

The reference to dates in DB2 is:
    http://www-106.ibm.com/developerworks/db2/library/techarticle/0211yip/0211yip3.html

-- Bubba
0
 

Author Comment

by:bganoush
ID: 11943226

Um... got it....

    select time(my_timestamp_field) from my_table where date(my_timestamp_field) =  (select current date from sysibm.sysdummy1)

Thanks...

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

636 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