Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Time in DateTime

I have a problem where my table stores the date as

2009-01-10 10:00:00.000

and I have another table which stores the date as

2009-01-10 12:00:00.000

I know they are not the same but the dates are, is there away in SQL to easily drop the time and keep just the dates for comparison? Thanks.

like image 339
Oliver S Avatar asked Jan 29 '09 20:01

Oliver S


People also ask

How do I remove the time from a datetime object in Python?

To remove the time from a datetime object in Python, convert the datetime to a date using date(). You can also use strftime() to create a string from a datetime object without the time. When working in Python, many times we need to create variables which represent dates and times.


3 Answers

Run this

SELECT DATEADD(dd, DATEDIFF(d, 0, Getdate()), 0)

change Getdate() to your column name to strip the date

BTW if you are doing a comparison it is better to do >= and < since it will perform better

like image 69
SQLMenace Avatar answered Oct 05 '22 23:10

SQLMenace


You can use this syntax:-

CAST(date_field AS DATE) as column_name

like image 7
Kunal Kabra Avatar answered Oct 05 '22 22:10

Kunal Kabra


Look here

You want the first one.

SELECT (CAST(FLOOR(CAST(GETDATE() as FLOAT)) AS DateTime))

Do not use any conversions to varchar, they are slow.

EDIT: @feihtthief - Here is a way to populate a table of dates (with 0 time) from a temp numbers table that is FAST. Edit it to save the numbers table if you want. They are handy as well.

DECLARE @DaysFromGoLive int                                                                                 
SET @DaysFromGoLive = (SELECT (DATEDIFF(dd,'10/01/2007',GETDATE()) + 1)) /* Days from go live is the starting date of the table */

 SELECT TOP 10950 --30 years of days                                                                        
        IDENTITY(INT,1,1) as N                                                                              
   INTO #Numbers                                                                                            
   FROM Master.dbo.SysColumns sc1,                                                                          
        Master.dbo.SysColumns sc2                                                                           

CREATE TABLE [dbo].[TableOfDates](                                                                      
    [fld_date] [datetime] NOT NULL,                                                                           
 CONSTRAINT [PK_TableOfDates] PRIMARY KEY CLUSTERED                                                         
(                                                                                                           
    [fld_date] ASC                                                                                            
)WITH FILLFACTOR = 99 ON [PRIMARY]                                                                          
) ON [PRIMARY]                                                                                              


INSERT INTO                                                                                                 
      dbo.TableOfDates                                                                                  
SELECT                                                                                                      
      DATEADD(dd,nums.n - @DaysFromGoLive,CAST(FLOOR(CAST(GETDATE() as FLOAT)) as DateTime)) as FLD_Date    
FROM #Numbers nums                                                                                          

SELECT MIN(FLD_Date) FROM dbo.TableOfDates                                                              
SELECT MAX(FLD_Date) FROM dbo.TableOfDates                                                              

DROP TABLE #Numbers                          
like image 6
StingyJack Avatar answered Oct 05 '22 23:10

StingyJack