Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how 1 6 2012 can be converted into 1june2012 please help me as soon as possible..
thnx

EDIT : OP intially posted the question with C# tag, some answer are based on that consideration.
Posted
Updated 19-Jun-12 1:19am
v4

Look at this solution in SQL:

SQL
DECLARE @date NVARCHAR(10) = '1 6 2012'

SET @date = REPLACE(@date, ' ', '.')

SELECT CONVERT(VARCHAR, CONVERT(DATETIME, @date, 104), 6)



Here are the diff. formats supported by SQL Server.

http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx[^]

The input you provided is not a valid format and I had to tweak it to get the correct format i.e. 106.

I replaced the blank spaced with a 'DOT'.

Hope this helps.
 
Share this answer
 
Comments
Sunny_Kumar_ 19-Jun-12 7:40am    
works fine. Good Job. my +5!
Manas Bhardwaj 19-Jun-12 7:45am    
thx!
Prasad_Kulkarni 19-Jun-12 8:21am    
Good one +5!
Manas Bhardwaj 19-Jun-12 8:25am    
thx!
Maciej Los 19-Jun-12 16:13pm    
Good work, my 5!
Hi Deepshikha,

In SQL Server, do like this ( I assume the data as you've mentioned to be -"1 6 2012")

SQL
select  CONVERT(datetime,(RIGHT(dateCol,4)+'-'+ SUBSTRING(dateCol,3,1) +'-'+LEFT(dateCol,1))) from testTable;


if you want it to be -"01 Jun 2012" then try this:

SQL
SELECT CONVERT(VARCHAR(11), CONVERT(datetime,(RIGHT(testCol,4)+'-'+ SUBSTRING(testCol,3,1) +'-'+LEFT(testCol,1))), 106) AS [DD MON YYYY] from testTable;


Hope this serves the purpose.

Cheers!
Happy Coding :)

Sunny_K
 
Share this answer
 
v2
Comments
Manas Bhardwaj 19-Jun-12 7:31am    
The output would be : 2012-06-01. OP wants to have ouput like 1june2012.
Please look at my answer.
[no name] 19-Jun-12 7:34am    
there is an error Type date is not a defined system type.please help me
Manas Bhardwaj 19-Jun-12 7:36am    
Did you look at Solution 3?
[no name] 19-Jun-12 7:39am    
@manas:ya i had looked bt its not helpful sir
Sunny_Kumar_ 19-Jun-12 7:40am    
answer updated... please have a look.
use convert.todatetime method for this
example:-
C#
string dt1 = Convert.ToDateTime(UrDate).ToString("ddMMMMyyyy");
 
Share this answer
 
Comments
[no name] 19-Jun-12 7:00am    
i want this code in sql server2005
Lousy Programmer 19-Jun-12 7:06am    
refer

http://www.w3schools.com/sql/func_convert.asp

http://www.w3schools.com/sql/func_date_format.asp
Manas Bhardwaj 19-Jun-12 7:15am    
thn why did you tag it for c#?
Manas Bhardwaj 19-Jun-12 7:14am    
Partially correct. You still need to parse the given date in correct format before converting.
I voted 4!
[no name] 19-Jun-12 7:18am    
@manhas:sorry sir
try this:

C#
string date = "1 6 2012";
            DateTime myDate;
            if(DateTime.TryParseExact(date, "d M yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate))
            {
                Console.WriteLine(myDate.ToString("ddMMMyyyy"));
            }


Read more about DateTime.TryParseExact [^]and Custom Formats [^]here.
 
Share this answer
 
Comments
[no name] 19-Jun-12 7:19am    
please tell me in sql server
Manas Bhardwaj 19-Jun-12 7:28am    
Posted a solution in SQL. Have a look!
 
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