Solved

Formatting a timestamp field on the select statement in DB2?

Posted on 2004-08-30
3
435 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now