Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dates issues

Author  Topic 

openlix
Starting Member

1 Post

Posted - 2009-10-12 : 17:58:28
I'm creating records to a file with fields datetime.
When I create, I don't want any values in the fields datatime but SQL Server assign 01/01/1900. I don't want it. How can I avoid that ?

If you have the answer, well I have another one. The ERP we are using save only the date, not the time, in the datetime fields. I don;t have the source code because I would like to know how to do that.

Any idea ?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-12 : 19:07:16
You can't use datetime in SQL Server 2005 if you only want the date or only the time. The datetime data type means you'll have both date AND time. There is no way around that with that data type. If you don't want that format, then use either char or varchar.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

lazerath
Constraint Violating Yak Guru

343 Posts

Posted - 2009-10-13 : 14:15:16
It's pretty easy to store a DATETIME but return just a date (just replace GETDATE() with your column name):

[CODE]SELECT CONVERT(VARCHAR(10),GETDATE(),101)

10/13/2009
(1 row(s) affected)[/CODE]

or time:

[CODE]SELECT CONVERT(VARCHAR(36),GETDATE(),108)

13:12:05
(1 row(s) affected)

SELECT CONVERT(VARCHAR(36),GETDATE(),114)

13:14:14:063
(1 row(s) affected)
[/CODE]

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-13 : 14:27:53
I'd suggest not doing that type of conversion in SQL Server, but rather let your application handle that since it's a formatting issue. Leave datetime data as is while inside SQL Server.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -