Title

Wednesday, 14 January 2015

select date part only in sql server


Im trying to get the date part of the date column to compare two dates, but could not get date part directly, i tried this Convert(date, getdate()) but this does not work with sql server 2005 as we are using that, and when i tried DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) it gives 2008-09-22 00:00:00.000 where i want only date part without time i-e 00, taken both from the posts: How to return the date part only from a SQL Server datetime datatype

What ihv done is convert(VARCHAR(10),'2014-04-21 00:00:00.000') and it returns 2014-04-21 but it is now in varchar.

Is there some way so that i may get the date part only in date type in sql server 2005.

Thanks in advance.

Answer

Try this

declare @Date date   set @Date = convert(VARCHAR(20),getdate(), 101)    print @Date

I don't know if this will work in sql 2005, but i think it will

Answer2

Is there some way so that i may get the date part only in date type in sql server 2005.

No, the date datatype was added in SQL Server 2008. The best you can do is to use a datetime with the time part 00:00:00.

No comments:

Post a Comment