Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
i am working in a project , where i have DATEPICKER in that,I selected the DATE and stored in SQLSERVER .. Its working FINE .. BUT i want DD-MM-YYYY format ..


so in query am using convert(varchar(50),@date,105) .But CONVERT function didn't work..it stored as date and time only ..PLease rack me out ...Thanks in advance..
Posted
Comments
Thanks7872 10-Dec-14 6:11am    
Date doesn't have format. Its Date's string representation which can be formatted. Your date&time.ToString("whatever format") is what you are looking for,i think.

Further, use proper datatypes while storing data into DB. e.g. DateTime for storing datetime values.
Bns Vigneshwaran 10-Dec-14 6:16am    
am using datepicker1.value while inserting into database... So obviously am getting answer in date time format .... so when insert i queried as convert(varchar(50),datepicker1.value,105) <---- it makes nothing .. So please help me to achieve...
Thanks7872 10-Dec-14 6:25am    
If you somehow got DateTime,why converting it then? Store it to db without any processing. Retrieve the same way at time of display. This time, you just need to convert to whatever format you want (ToString).

SQL dates don't have a format - they (just like the C# DateTime structure) are stored as a number of milliseconds since a specific point in time. And they only ever acquire a "format" such as dd-MM-yyyy or MM/dd/yyyy when they are presented to the user (which requires a string conversion). This conversion to a formatted strign is done as late as possible (and conversion from a user input string done as early as possible) so that it can be presented in the format the user prefers, and specified by his systems selected Culture. Since SQL doesn't know what PC (or tablet, or phone) the data is going to be displayed on, it is a bad idea to store info in a formatted strings, or even to convert it to a string at the SLQ layer - it needs to be done at the presentation layer, be that a Winforms form or a website.

So pass your DateTime value from the date picker directly to SQL via a parametrised query, and return your stored values as DateTime to your C# code for formatting there.
 
Share this answer
 
That's right.. "SQL dates don't have a format"
if you want to show SQLDB date time in text box, then you must convert it to the String..


Use..
C#
textbox1.Text=(DbdatetimeCoumnValue).toString("dd-MM-YYYY")


For C# Custom Date and Time Format Strings
see
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^]
 
Share this answer
 
Simply convert your DatePicker value to String and then store it to database.

C#
string date = Convert.ToDateTime(datePicker.Value).ToString("dd-MM-yyyy");
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 10-Dec-14 6:37am    
Wow, someone has devoted and someone has Upvoted my solution.
Bns Vigneshwaran 10-Dec-14 6:39am    
You Genius !! Thanks for ur help ..
Praveen Kumar Upadhyay 10-Dec-14 6:42am    
Use below code. If you are using Datepicker in Windows Application

datepicker1.Value = Convert.ToDateTime(dr["h_dob"].ToString());
Bns Vigneshwaran 10-Dec-14 6:43am    
Thank you genius
Praveen Kumar Upadhyay 10-Dec-14 6:44am    
Welcome buddy and thank you for accepting the answer and I am not Genius just trying to solve few small small issue here.
hi,

use this

select convert(date,cast(@date as datetime),103)

date format-- dd/mm/yyyy-->>103
 
Share this answer
 
v2

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