Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 9th, 2008, 06:31 PM
Newbie
 
Join Date: Sep 2008
Posts: 1
Default How to form a date value from decimal or smallint values

Hi,
I have a table with fields like yr and month of type decimal and smallint respectively. I want to compare the date value formed by the yr and the month values in the tables by taking the day by default as 01. So the date should look like yr-month-01 in DB2. How do I combine these values and form a datre value out of them?


Please reply if you know the solution.

Thanks,
Nidhi.
Reply
  #2  
Old September 10th, 2008, 07:01 AM
Newbie
 
Join Date: Jan 2007
Posts: 27
Default

What you can try to do is a concat and then cast it as a date. so it should look something like:

concat (char(dat), char(month), '01')
and then u can use date while comparing

date(concat (char(dat), '-',char(month), '-','01') )

I am not sure whether this will work or not. but worth a try. other experts can also response
Reply
  #3  
Old September 10th, 2008, 04:56 PM
docdiesel's Avatar
Moderator
 
Join Date: Aug 2007
Location: Munich
Posts: 281
Default

Hi,

you'd better use || instead of concat. The latter is limited to two parameters only, so you'd have to do a lot of contat's. Furthermore, you've got to cast the decimal into an integer. Otherwise you'll get a decimal point (or comma, depends on local settings) after the year. Last but not least you've got to strip all the blanks from the casted chars with a rtrim(). Otherwise you'd get something like "2008 -10 -01".

Expand|Select|Wrap|Line Numbers
  1. select
  2.   date(
  3.     rtrim(char(integer(yr)))
  4.     || '-'
  5.     || rtrim(char(month))
  6.     || '-01'
  7.   ) as gen_date
If you've got to use this often, you should think about adding an autogenerated column based on this code to your table. If needed real often, an index on that column would be very recommended.

Regards

Doc Diesel
Reply
  #4  
Old September 10th, 2008, 05:01 PM
docdiesel's Avatar
Moderator
 
Join Date: Aug 2007
Location: Munich
Posts: 281
Default

P.S.: You'll find a little howto about autogenerated ('generated always') columns in this thread.

Regards,

Doc Diesel
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles