Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting a timestamp field on the select statement in DB2?

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

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 article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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