Solved

DB2 To/From datetime - string date conversions

Posted on 2007-04-06
6
13,851 Views
Last Modified: 2008-01-09
Problem with DB2 DATETIME data retrieved and placed in a vb.net  string variable, then attempting to convert back to datetime in an update query.

Example:

A Datetime column has data that in a DB2 table.  When retrieved and placed in a string variable, it looks like this:  1/1/1994 12:00:00 AM.

What do I need to do in the update query to have it in correct datetime format?
Could you also show a good way convert both to and from database (both directions)


 
0
Comment
Question by:garyinmiami2003
[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
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:kerryw60
ID: 18864503
A couple of different ways to do it.  Could convert to a string value then use to build your query:

Dim strDate as String

strDate = DatePart(DateInterval.Month, myDate).ToString()
strDate = strDate & "/" & DatePart(DateInterval.Day, myDate).ToString()
strDate = strDate & "/" & DatePart(DateInterval.Year, myDate).ToString()
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 18864710
db2's datetime format is called timestamp
the format of a timestamp is
YYYY-MM-DD-HH24.MI.SS.NNNNNN' where nnnnnn are nanoseconds

in order to convert from / to that value you can either use vb code, or use some db2 functions
db2 has the following built in functions to handle timestamp values :
year - extract the year
month - extractd the month
day- extract the day

plus you can use string function to built up the date such as
substr, posstr

can't really help you with the vb code, but i'm sure you know vb, if you asked this quesiton
0
 

Author Comment

by:garyinmiami2003
ID: 18864767
momi sabag:

 the data example provided with the question is the result of DB2 data in DATE format (my orignal mistake)   being moved to a datagrid and later in a string variable.  Now this character string I provided:
1/1/1994 12:00:00 AM is the way it looks in the text file.  I need to update this back to the db2 table.

Could you provide the syntax to do that including the update query for the one field.

And over and above the question Can you suggest a good way to edit the data since it could be modified by the user?  (the last is extra but appreciated)  
 
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 37

Expert Comment

by:momi_sabag
ID: 18864878
hi

can you tell me exactly the format of the date that the user provides ? since you wrote 1/1/1994  i can't know which is the month and which is the day
plus, in vb, can you set the date format of the data grid or does vb have a constant format that cannot be changed ?
0
 

Author Comment

by:garyinmiami2003
ID: 18864927
it is displayed in USA format MM/dd/yyyy
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 18866169
hi
the db2 date format is dd/mm/yyyy
i would try to do it in the vb code, since it would be rather complex in sql, but here goes

update table
set date_col = substr(your_column,   posstr(your_column, "/") + 1,   posstr( substr(your_column, posstr(your_column, "/")+1), "/")  - posstr(your_column, "/")  - 1) || "/" || substr(your_column, 1, posstr(your_column, '/") - 1) || "/" ||
substr(your_column, posstr( substr(your_column, posstr(your_column, "/")+1), "/") +1, 4)

when
your_column - the name of the host variable
posstr(your_column, "/") - this returns the index of the first "/"
substr(your_column, posstr(your_column, "/")+1) - returns the string after the first "/"
posstr( substr(your_column, posstr(your_column, "/")+1), "/") - returns the index of the second "/"
substr(your_column, posstr( substr(your_column, posstr(your_column, "/")+1), "/") +1) returns the string after the second "/"

or as i said, you better of doing it in the vb code
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groupbox Control ? 2 39
Import a excel sheet in a grid 2 44
How do i create a simple array in a public class? 6 36
Open telerik datetime picker programmatically 13 42
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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