Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i insert date and its inserted as "yyyy-mm-dd" format
but i want to insert as "dd-mm-yyyy" format
please tell me simple coding for
how to insert date in sql server as "dd-mm-yyyy" format
Posted
Updated 5-Mar-18 6:44am
Comments
Kornfeld Eliyahu Peter 4-Feb-14 6:31am    
Oh! That hurts! You actually proposes to use string for storing date value, just because of visual formatting issue! Why?! What the connection?!

Don't insert a date as a specific format. Dates should be stored using the appropriate Date/DateTime types, and they should be stored as a universal format. It's a common misconception that you should be storing dates in a particular format - this is a bad practice to get into. What date was 10/11/2013? Was it the 10th November 2013 or the 11th October 2013? By storing the date in a universal format, you remove ambiguity and you then do the date formatting work at the place it should be done - at the UI after you have retrieved it, where it can be made to follow local date/time conventions.
 
Share this answer
 
v2
SQL date is a binary field (stores binary value regardless of formatting).
So you do not store date in SQL by any format - only displays it in your desired format in your application.
you may use this at the SQL level - http://technet.microsoft.com/en-us/library/hh213505.aspx[^]
or simething like this inside the application
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^]
 
Share this answer
 
Unless you are inserting it as a string (varchar & friends) the date is not formatted, i.e. the internal representation has no notion of 'format'.
Probably you are performing a wrong conversion either from string to date or from date to string in your code.
 
Share this answer
 
CONVERT(date, @Date, 105)

Use this expression to convert date before insert.
 
Share this answer
 
Comments
Ankur\m/ 4-Feb-14 6:38am    
How is it different from my answer. What's the point posting the same answer again?
C#
DateTime dt = DateTime.Now;
        string format = "dd-mm-yyyy";
        string dtTObeInsertedInSQL = dt.ToString(format);
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 4-Feb-14 6:32am    
And - it still binary in SQL (it's a date)!!!
This format is not supported for insert in SQL Server date field by default. You will need to use CONVERT to do it

SQL
INSERT INTO TableName (DateColumn) VALUES (CONVERT(date, '13-02-2014', 105))

105 refers to style. Check this for more details - CAST and CONVERT (Transact-SQL)[^]

Hope that helps!
 
Share this answer
 
Comments
Ankur\m/ 4-Feb-14 6:37am    
The down-voter here, could you please give a reason for the down-vote?
Manish Dalwadi 4-Feb-14 6:47am    
"@date date" is my stored procedure's parameter and "dob" is my table's field
and i tried it but showing me an error.

INSERT INTO birthdate (dob) VALUES (CONVERT(@dob, 'dd-mm-yyyy', 13));

i know i am wrong...
its first time for me...
sir, so pls avoid my mistakes
Ankur\m/ 4-Feb-14 6:54am    
It should be:
INSERT INTO birthdate (dob) VALUES (CONVERT(date, @dob, 13));

Just confirm the style no.
Manish Dalwadi 4-Feb-14 6:57am    
i use it as
INSERT INTO birthdate (dob) VALUES (CONVERT(date, @dob, 13));
..
query is complete but date is inserted as
1990-02-02

and i want it as 02-02-1990.
.
pls help
 
Share this answer
 
 
Share this answer
 
v2
Try This


SELECT CONVERT(VARCHAR(10),GETDATE(),103)


By changing the last number you can get different formats of dates..
 
Share this answer
 
v2
CONVERT(VARCHAR(8), GETDATE(), 105)
 
Share this answer
 
Comments
Maciej Los 5-Mar-18 13:03pm    
Few years too late...
If you want to avoid down-votes, you shouldn't post an answer to such old (and resolved) question.

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