473,418 Members | 2,009 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,418 software developers and data experts.

Convert DECIMAL to DATE

Ray
I have a table with some audit date and time columns. Problem is the
developer who stored the data left them as DECIMAL type instead of DATE
and TIME. Is there a way I can convert the DECIMAL type to DATE or
TIME? The column data is in the date form YYYYMMDD (i.e. 20060308 =
March 8 2006).

I want to get the data into a DATE type. I tried
TO_DATE('20060308','YYYYMMDD') but I cannot get it to work.

What else can I do to conver the data type?

Thanks,
Ray

Mar 8 '06 #1
5 43740
Ray wrote:
I have a table with some audit date and time columns. Problem is the
developer who stored the data left them as DECIMAL type instead of
DATE and TIME. Is there a way I can convert the DECIMAL type to DATE
or TIME? The column data is in the date form YYYYMMDD (i.e. 20060308 =
March 8 2006).

I want to get the data into a DATE type. I tried
TO_DATE('20060308','YYYYMMDD') but I cannot get it to work.

What else can I do to conver the data type?

Thanks,
Ray


Urgh, horrible one, but this seems to work (MYDATE is the field
containing the DECIMAL value):

DATE(INSERT(INSERT(LEFT(CHAR(MYDATE),8),5,0,'-'),8,0,'-'))

There must be a more elegant solution though...
HTH,

Dave.

--

Mar 8 '06 #2
Dave Hughes wrote:
Ray wrote:
I have a table with some audit date and time columns. Problem is the
developer who stored the data left them as DECIMAL type instead of
DATE and TIME. Is there a way I can convert the DECIMAL type to DATE
or TIME? The column data is in the date form YYYYMMDD (i.e.
20060308 = March 8 2006).

I want to get the data into a DATE type. I tried
TO_DATE('20060308','YYYYMMDD') but I cannot get it to work.

What else can I do to conver the data type?

Thanks,
Ray


Urgh, horrible one, but this seems to work (MYDATE is the field
containing the DECIMAL value):

DATE(INSERT(INSERT(LEFT(CHAR(MYDATE),8),5,0,'-'),8,0,'-'))

There must be a more elegant solution though...
HTH,

Dave.


Well, I've tried a couple more ways and it does seem that the above is
the simplest way to perform the conversion.

Incidentally, it seems that TO_DATE (aka TIMESTAMP_FORMAT), while
initially appearing to be a really cool function that might be able to
parse all sorts of formats and convert them into TIMESTAMP or DATE
values is in fact the most useless function I've ever come across! When
the documentation states:

Valid format strings are:

'YYYY-MM-DD HH24:MI:SS'

It really means exactly that. In other words, "the *ONLY* valid format
string is". I've tried all sorts of other combinations, but even things
as simple as changing the date-separator, re-ordering the date
components, or removing the time portion from the template fails:

db2 => VALUES TO_DATE('2006/03/08 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
SQL0171N The data type, length or value of argument "2" of routine
"SYSIBM.TO_DATE" is incorrect. SQLSTATE=42815

db2 => VALUES TO_DATE('03-08-2006 00:00:00', 'MM-DD-YYYY HH24:MI:SS');
SQL0171N The data type, length or value of argument "2" of routine
"SYSIBM.TO_DATE" is incorrect. SQLSTATE=42815

db2 => VALUES TO_DATE('2006-03-08', 'YYYY-MM-DD');
SQL0171N The data type, length or value of argument "2" of routine
"SYSIBM.TO_DATE" is incorrect. SQLSTATE=42815

Which kind of begs the question, why even bother having the second
argument if it can only take a single value?! Bizarre...
Dave.

--

Mar 8 '06 #3
How about this?
DATE(TRANSLATE('ABCD-EF-GH',DIGITS(MYDATE),'ABCDEFGH'))
or
DATE(INSERT(INSERT(DIGITS(MYDATE),5,0,'-'),8,0,'-'))

(Assuming data type of MYDATE is DEC(8,0))

Mar 9 '06 #4
Tonkuma wrote:
How about this?
DATE(TRANSLATE('ABCD-EF-GH',DIGITS(MYDATE),'ABCDEFGH'))
or
DATE(INSERT(INSERT(DIGITS(MYDATE),5,0,'-'),8,0,'-'))
Ooh, very nice! I don't think I've ever used the TRANSLATE function
before... That's certainly a recipe I'll keep in mind for future use :-)
(Assuming data type of MYDATE is DEC(8,0))


Yes ... that's why I added the LEFT(..., 8) call in my one, just to
make sure any decimals got stripped off the end, but that could just as
easily be applied to either of your formulae above.
Thanks,

Dave.

--

Mar 9 '06 #5
Ray
Excellent. Thanks a lot to everyone who posted, this was killing me.

Mar 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tamrakar | last post by:
Please let me know how I can Load a packed decimal date coming from a file from mainframe be loaded into a Oracle table date column? Any help will be appreciated. Thanks,
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
1
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
13
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the...
13
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
2
nirmalsingh
by: nirmalsingh | last post by:
Hai all, i want to convert decimal to time. for example if i have decimal number 09.6 then i need it as 10.00 and also decimal 10.118 as 11.58. How to convert this? Thanx in advance with Cheers...
1
by: Vitaliy | last post by:
Hi I got this wired exception periodically (Python 2.5, Django based application) what does it mean ?
1
by: smdmca | last post by:
I have Julian date, I want to convert it into date. Is there any function in MySql to convert Julian date to date eg- Julian Date- 2455116 Date - Oct - 12, 2009
4
by: Rolandas | last post by:
Hi, Can anyone help me? I need a code in VBA which would convert decimal numbers to the other system bases on number 36. e.g. in this page...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.