开发者

inserting only date in a column in sql server

Is there a way to insert only date in a datetime column in sql server without the time? for example

date (datetime)

===============

12-0开发者_如何学C1-2000

16-02-2000

or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.


my solution is to store it as varchar and convert it to datetime whenever needed

SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only

or

also check this post about creating date type :

create user defined data types:

create type Date from dateTime

http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx


If you are using SQLServer 2008 you can use the date data type.

The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.

Select  Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable


Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.

You can change format of date format in sql queries or in your app.

Here is a list on date formats in sql

http://www.sql-server-helper.com/tips/date-formats.aspx

Here's a link on date data types

http://databases.about.com/od/sqlserver/a/date_time.htm

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜