Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I need to fetech some datafrom DB2 and paste in SQL. I am able to do that but some problem in date format.

Date format in DB2 is like 2003-09-23 12:32:09.095499

but when i insert into sql its became 9/23/2003 12:32:09 PM

actually my DB2 field is a string not a date. but sql converts it into date

i am feteching data from DB2 with help of dataset and datatable. In the dataset itself dateconversion occur.

How can i solve this

My SQL quey for feteching data from DB2
SQL
SELECT KEY_TMS, CAT_TRDCTL, DT2_BILL, CAT_BILL, DT2_FROM, KEY_ACCT, DT2_ME, AMT_MKTVAL FROM TBL_BILL 


this is how i am passing value to sql
C#
cmd.Parameters.Add("@KEY_TMS", SqlDbType.VarChar).Value =dt.Rows[i]["KEY_TMS"].ToString();



Edited***
As per suggestion i made date formating
but millisecond portion is not working as expected

C#
keytms_date = Convert.ToDateTime(dt.Rows[i]["KEY_TMS"].ToString());
cmd.Parameters.Add("@KEY_TMS", SqlDbType.VarChar).Value = keytms_date.ToString("yyyy-MM-dd hh:mm:ss.ffffff");

its showing 000000 insted of 095499
how can i solve this issue
Posted
Updated 19-Mar-15 2:02am
v2

1 solution

To fix it change .ToString() and format the string.

Take a look at ustom Date and Time Format Strings

I suppose you have declared this field as varchar instead of datetime in SQL database.
 
Share this answer
 
Comments
jinesh sam 19-Mar-15 8:04am    
Thanks for your support
I made changes as per your suggestion but millisecond portion is not working
i have updated the question please look into it
Joan Magnet 19-Mar-15 8:11am    
If KEY_TMS is a string you shouldn't convert it to datetime.

Try to assign it to a string variable without conversion.
jinesh sam 19-Mar-15 8:18am    
let me check
jinesh sam 19-Mar-15 8:23am    
string date = dt.Rows[i]["KEY_TMS"].ToString();

but its displays with am pm

i cannot use like this its showing an error
string date = dt.Rows[i]["KEY_TMS"].ToString("yyyy-MM-dd hh:mm:ss.ffffff");
Joan Magnet 19-Mar-15 8:26am    
Can check dt.Columns["KEY_TMS"].DataType ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900