Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting a timestamp field on the select statement in DB2?

Posted on 2004-08-30
3
Medium Priority
?
461 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 750 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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 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