tags:

views:

46

answers:

3

I have my date values in postdate column in articles table in sql database table. It stores all date as well as time. I would like to get the date part only. i.e from 2010/07/01 12:45:12 i would likfe to get 2010/07/01 in my view

+1  A: 

you can use either of the two

select substring(getdate(),0,10)

use

 select convert(varchar,getdate(),111)

see this for more formats

Shantanu Gupta
+6  A: 

With SQL Server 2008, you can cast the value as DATE. With previous versions, you can use a trick over DATEADD:

DECLARE @d DATETIME;

SELECT @d = '2010-07-22 12:45:22';

-- all versions of sql server
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, @d), 0);
-- sql server 2008
SELECT CAST(@d AS DATE);

Greets Flo

Florian Reischl
amazing, I never knew about that DATEADD trick before! I was always converting to/from a char(10) to 'fake it'.
Coxy
Thanks :-)! I've learned that on SCC. Since then, I've already used it hundred times.
Florian Reischl
thnx gr8 trick it worked
KoolKabin
A: 
SELECT convert(varchar, getdate(), 111)

How to format datetime & date in Sql Server 2005

Muhammad Kashif Nadeem