Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
How do I store dates only by using ASP.NET in SQL Server 2005.



VB
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim medate As Date = TextBox1.Text
    Dim st As String = ConfigurationManager.ConnectionStrings("testingConnectionString").ToString
    Dim con As New Data.SqlClient.SqlConnection(st)
    con.Open()
    Dim q As String = "insert into mytest values('" & medate & "')"
    Dim com As New Data.SqlClient.SqlCommand(q, con)
    com.ExecuteNonQuery()
    con.Close()


End Sub



it is my code where i am taking date from calender n store in database
but it stores date with time 12:00am but i want to store date only or time 0:00
how can i do
Posted
Updated 1-May-11 1:39am
v3

please be clear....

I thought you have DateTime DataType in your Code and you need to extract Only Date value from this datetime field or DataType.

Refer below link,

[^]
 
Share this answer
 
Comments
adilmemon 29-Apr-11 11:02am    
how to store only date instead of date with time in sql server 2005 with 2008
plz help i am short on time quick
Added to Partik's answer in .Net the DateTime object's toShortDateString() gives you a date only string and if you want it in specific format the you can use the ToString method with specific format. Example ToString("yyyy/M/dd")
 
Share this answer
 
Comments
adilmemon 28-Apr-11 12:38pm    
through this only in asp.net date will be ready but how to store it in sqlserver 2005
Albin Abel 28-Apr-11 12:54pm    
If your sql table column is of type 'date' then you can pass this string in your insert/ update query. Are you asking about how to formulate the query and execute in asp.net?
adilmemon 1-May-11 7:41am    
in sqlserver 2005 there is no date datatype there is datetime only i have modify the question so check it out n give the solution thanx..
Albin Abel 1-May-11 15:46pm    
You are right. Sql server 2008 supports a date type. In that case you can store that dummy time string, but trim it while retrieving. Alternatively instead of storing datetime, convert the date to timestamp (millseconds) and store. While retrieving convert back to date. You want to store only date part, but you haven't explained why you want that?. When you modify your question add explanation why you want that. So you may get good suggestions. Good luck
adilmemon 2-May-11 10:51am    
thanx i want bcoz i generate reports on based on that
If you plan to store date only , then you have to make your datatype as varchar. In datetime you cant store only date. If you pass only date it autometically place 12.00 as a default time.
 
Share this answer
 
Comments
adilmemon 1-May-11 12:52pm    
IF I WANT TIME TO BE 0:00 THEN

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