Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return only date

Status
Not open for further replies.

Ofina

Technical User
Feb 25, 2004
156
US
Why can I not find a function that will return 12/14/2011 for 12/14/2011 10:12:15? And have it remain as a date and not change to a string?
 
DECLARE @Datevalue DATETIME
SET @Datevalue='12/14/2011 10:12:15'

--In SQLServer2008 (not sure about 2005)
SELECT CONVERT(DATE,@DateValue)

--In SQLServer 2000
SELECT CONVERT(DATETIME,FLOOR(CONVERT(DECIMAL(19,4),@DateValue)))

Viewer, scheduler and manager for Crystal reports.
Send your report everywhere.
 
RTag is indicating the Date type is not available for long. I am sure, it is introduced in SQL2008, not 2005. If you run a database in compatibiliy mode that could also mean you only get dattimes or smalldatetimes. But I am not sure about that.

What I'm sure about is Date was introduced in MSSQL2008 and works for databases started in that server.

And then you can do as RTag suggests, I'd do:

Code:
DECLARE @Datevalue as Date;
Declare @Datevalue as Date;
Set @Datevalue = Convert(Date,Getdate())

--or

Sekect @Datevalue = Convert(Date,datetimefield) FROM ...

Bye, Olaf.

 
Sorry for the obvious errors...
Code:
Declare @Datevalue as Date;
Set @Datevalue = Convert(Date,Getdate())

--or

Select @Datevalue = Convert(Date,datetimefield) FROM ...

Bye, Olaf.
 
I should have said that we have SQL 2005.

That last one worked, thanks. Though it didn't so much strip the time portion as make it all 00:00:00. However, it still helps me achieve what I was going for.
 
Datetime time always has both date and time portions. If you want a date only, in SQL 2005 and less you can only convert to character.

See cast and convert functions in BOL.

PluralSight Learning Library
 
Datetime contains information about date and time, but the way how this information is presented is up to you. If you need to print the date somewhere (paper, pdf, excel as text field etc.) you can use convert with format option:

DECLARE @Datevalue DATETIME
SET @Datevalue='12/14/2011 10:12:15'
SELECT CONVERT(VARCHAR@Datevalue,101)
--101 will set the format to mm/dd/yyyy

If you need to compare the datevalue to another datevalue then use the formula from the previous post to cut the time part.

Viewer, scheduler and manager for Crystal reports.
Send your report everywhere.
 
Well that would at least explain why I couldn't find one (I knew about the one that converts to varchar 101); it doesn't exist.

It does exist in 2008 though?
 
DATE in SQLServer 2008 is 3 bytes while DATETIME is 8 bytes. I guess the only one reason you may want to use DATE is if you are concerned about the size of the table.

Viewer, scheduler and manager for Crystal reports.
Send your report everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top