JoeSnyderJr
asked on
How to remove time from MSSQL datetime field
I have several tables with seconds minutes etc in datetime field. We want to truncate the field value to just yyyy-mm-dd. What select statement can we use to accomplish this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Cast(Left(GetDate, 12) as DateTime)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes the convert to varchar is good only if you are trying to do display the date, if you want to store values to another table or you want to apply sorting on retrieved field, i dont think you wana use varchar.
The ways I suggested can be used if you want to keep field as datetime but ignore time so a date 2009-01-11 14:04:41.007 will be converted to 2009-01-11 00:00:00.000.
The ways I suggested can be used if you want to keep field as datetime but ignore time so a date 2009-01-11 14:04:41.007 will be converted to 2009-01-11 00:00:00.000.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tested solutions offered and any one of these works to truncate datetime to date as requested
CAST((
STR( YEAR( GETDATE() ) ) + '/' +
STR( MONTH( GETDATE() ) ) + '/' +
STR( DAY( GETDATE() ) )
)AS DATETIME )
OR this
Cast(Left(GetDate, 12) as DateTime)
Thanks to all for solutions
OR these
select Convert(nvarchar(10),getda te(),20)
select Convert(nvarchar(10),getda te(),120)
select Convert(nvarchar(10),getda te(),21)
select Convert(nvarchar(10),getda te(),121)
select Convert(nvarchar(10),getda te(),126)
select Convert(nvarchar(10),getda te(),127)
OR this
dateadd( day, datediff( day, 0, getdate() ), 0 )
CAST((
STR( YEAR( GETDATE() ) ) + '/' +
STR( MONTH( GETDATE() ) ) + '/' +
STR( DAY( GETDATE() ) )
)AS DATETIME )
OR this
Cast(Left(GetDate, 12) as DateTime)
Thanks to all for solutions
OR these
select Convert(nvarchar(10),getda
select Convert(nvarchar(10),getda
select Convert(nvarchar(10),getda
select Convert(nvarchar(10),getda
select Convert(nvarchar(10),getda
select Convert(nvarchar(10),getda
OR this
dateadd( day, datediff( day, 0, getdate() ), 0 )
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Open in new window