473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Sep 9 '08 #1
3 3486
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
Sep 10 '08 #2
docdiesel
297 Expert 100+
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
Sep 10 '08 #3
docdiesel
297 Expert 100+
P.S.: You'll find a little howto about autogenerated ('generated always') columns in this thread.

Regards,

Doc Diesel
Sep 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iver Erling Årva | last post by:
I have come across a problem with the onKeyDown event in some of my forms. I'm using onKeyDown in <form> as a standard method to open my help screen system throughout my system, but I have...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
2
by: LitelWang | last post by:
I need this function : CheckDate('2002-02-29') return false CheckDate('2002-02-28') return true How to write ? Thanks for any advice .
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
11
by: shsandeep | last post by:
I used the following query to retrieve the date in dd-mon-yyyy format. db2 => SELECT RTRIM(CHAR(DAY(COVG_TYP_STRT_DT))) || '-' || RTRIM(MONTHNAME(COVG_TYP_STRT_DT)) || '-' ||...
2
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3...
6
by: dream2rule | last post by:
<form action='' method='post' name='create_table_form' id='create_table_form' > <table border='0' cellpadding='0' cellspacing='0' width='95%'><tr><td><br /> <table border='0' cellpadding='0'...
10
by: Gilles Ganault | last post by:
Hello Out of curiosity, is there a smarter, easier way to read data sent by a form, and save them into a database? I have about 20 fields, and it'd be easier if I could just use a loop to go...
9
by: Frank Swarbrick | last post by:
I've probably asked this before, but I can't remember the answer! One can use the DECIMAL function to convert a date to a decimal. For instance values decimal(current_date) returns 20080528. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.