Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I need help on the following.I have a column 'Date' in Staging table with the following
Data type=varchar(50)
Format/Style='dd/mm/yy'
Example='18/11/07'

I need to convert 'Date' column to the following .This conversion would take place from a staging table to a destination table.
Data type = datetime
Format/Style ='mm/dd/yy'
Example='11/18/07'

I have tried the following query
SQL
SELECT Date,
CONVERT(varchar(50),Date,101) 
FROM staging_table 


BUT NOTHING CHANGED .Where am I going wrong ?
Posted
Updated 19-Oct-12 0:00am
v2
Comments
MT_ 19-Oct-12 5:54am    
This is simple select query , what is the output ?
Rachana Gaur 19-Oct-12 5:57am    
Nothing same values gives us
Rachana Gaur 19-Oct-12 5:58am    
what should I do for this
Richard MacCutchan 19-Oct-12 5:58am    
What do you mean by "NOTHING CHANGED"? Your SELECT statement should have returned a datetime value, what exactly did you see?
Rachana Gaur 19-Oct-12 6:03am    
I will see same value like we have 16/03/2012 & After this query I had get 16/03/2012

Rachana,

This is because your SQL is set in different format. Now you Convert has input varchar and output also varchar, hence it is not converting correctly.

First you have to cast it to datetime and then convert it to appropriate date format. See below

SQL
set dateformat dmy
select convert(varchar(50),cast('18/11/07' as DateTime),101)


Hope that helps. If it does, mark the answer as solution and/or upvote.

-Milind
 
Share this answer
 
v3
Comments
Rachana Gaur 19-Oct-12 6:13am    
what type of update query will be use

Give an example
You basically need to

insert into destinationtable select from staging table.


from what I gather this
SQL
SELECT Date,
CONVERT(varchar(50),Date,101) 
FROM staging_table 


Will convert it to varchar(50), you need to convert the char from the staging table to a datetime.

SQL
SELECT Date,
CONVERT(datetime,Date,101) 
FROM staging_table 


or something similar.
(Apologies if the syntax is not 100% correct, I have no SQL-Server to test with.)

Hope this helps.

PS: IMO this is not a C# question, but an SQL/Database question.
 
Share this answer
 
SELECT convert(varchar, getdate(), 101) AS Date -- Date is temporary Column name
 
Share this answer
 

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