Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to store only date from datetime data type in sql server 2005
Posted
Comments
[no name] 4-Sep-12 7:31am    
you can't store date without time in sql server. time will be 00:00:00.000 every time.

 
Share this answer
 
Comments
Espen Harlinn 4-Sep-12 8:48am    
5'ed!
Prasad_Kulkarni 5-Sep-12 0:02am    
Thank you Espen!
Manas Bhardwaj 4-Sep-12 16:14pm    
Good 5+
Prasad_Kulkarni 5-Sep-12 0:02am    
Thank you Manas!
You could use convert if you use datetime or varchar for storing date

In Sql server 2008 you have date datatype

SQL
SELECT CONVERT(datetime, '12-1-2012', 103)
SELECT CONVERT(varchar(12), getdate(), 103)
 
Share this answer
 
v2
DateTime datatype storage will have timestamp in it.

Either you convert it into desired format at the time of retrieval OR save data as a string(varchar).
 
Share this answer
 
Hi,
Try this:
SQL
SELECT CONVERT(VARCHAR(30), CURRENT_TIMESTAMP, 104)

Refer this[^] for different formats.

--Amit
 
Share this answer
 
sql server 2005 does not serve only 'Date' datatype
'Datetime' is datatype to store date & date-time values as per need,

SQL
Insert into tbl(dt) values(getdate()) 
-- result => 04/09/2012 6:20:24 PM

SQL
Insert into tbl(dt) values((CONVERT([datetime],CONVERT([varchar](10),getdate(),(102)),0))) 
-- result => 04/09/2012 12:00:00 AM
--time is 12:00:00 AM so it is negligible and in this case it will consider value as only date 


problems, while filling datetime values instead of only date values [while actually time is not require in some field],

example, voucher_date -> this field i want date only (e.g 04/09/2012 12:00:00 AM) not date-time (e.g 04/09/2012 6:20:24 PM)
if I will save datetime both
then selecting records using where condition like below will not give result

SQL
where dt='2012-09-04'
--Note: where dt>='2012-09-04' and dt<'2012-09-05' will work but direct comparison using '=' operator will not work 

Happy Coding!
:)
 
Share this answer
 
SQL
insert into Emp (Empid, Bdate
values (200, '2005-01-01')
 
Share this answer
 

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