Solved

Formatting a timestamp field on the select statement in DB2?

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…
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…

830 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