Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get DATE part of the DATETIME


Get DATE part of the DATETIME

Author
Message
Stefan Albl
Stefan Albl
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 43
In SQL-Server 2008: usenew datatype date

DECLARE @DateOnly DATETIME
SET @DateOnly = CAST(CAST(GetDate() AS Date) AS DATETIME)
SELECT @dateonly
Mike.Flannery
Mike.Flannery
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 24
SELECT CAST(CAST(GETDATE() AS VARCHAR(11)) AS DATETIME) AS Today
brosspaxedi
brosspaxedi
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 261
Slightly tangential, but I like to cast as SMALLDATETIME because I usually cut and paste my results into Excel; If I use a datetime, Excel shows 00:00 or somthing but if I use smalldatetime, it shows it as a date without my formatting it.

*******************
What I lack in youth, I make up for in immaturity!
brosspaxedi
brosspaxedi
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 261
Also, you can add or subtract days like so:

DECLARE @DateOnly DATETIME
SET @DateOnly = CAST(FLOOR(CAST(GetDate() AS FLOAT)+1) AS smallDATETIME);
print @DateOnly



Prints just like the original did, but a day later. Subtract 2 and you get two days earlier.

Since DateAdd is apparently high overhead, it seems like a good alternative.

*******************
What I lack in youth, I make up for in immaturity!
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
Any particular reason to resurrect a discussion that's 3 years old? Or just bored?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
brosspaxedi
brosspaxedi
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 261
It was the "Featured Script" in today's newsletter.

*******************
What I lack in youth, I make up for in immaturity!
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
Ah. That's what I get for not reading the newsletter till after work. :-)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Jonathan AC Roberts
Jonathan AC Roberts
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 1836
I use these as I figure it's better to use SQL servers internal function than assume Datetime will always be a float:

DECLARE @DateOnly DATETIME
SET @DateOnly = CAST(CONVERT(varchar,GetDate(),112) AS DATETIME)
SELECT @DateOnly
SET @DateOnly = CAST(CONVERT(varchar(10),GetDate(),121) AS DATETIME)
SELECT @DateOnly


brosspaxedi
brosspaxedi
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 261
Adding an integer to your code works like DateAdd() as well.

*******************
What I lack in youth, I make up for in immaturity!
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
I've done pretty heavy testing on this. The DateAdd-DateDiff method has been the fastest, and has the advantage of being both backwards and forwards compatible.

The Int conversion has rounding issues and simply shouldn't be used.

The float conversion has problems with the new data types in SQL 2008, and thus isn't forwards compatible with that.

The string conversion is a performance killer (takes an average of 4-10 times as long as native date functions).

Conversion to Date is the fastest and is forwards compatible, but isn't backwards compatible.

In situations where backwards compatibility isn't an issue, conversion to Date is best. Where backwards compatibility might matter, the nested date function method is best.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search