Created
March 24, 2014 23:15
-
-
Save bennadel/9751387 to your computer and use it in GitHub Desktop.
Getting Only the Date Part of a Date/Time Stamp in SQL Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CAST( | |
( | |
STR( YEAR( GETDATE() ) ) + '/' + | |
STR( MONTH( GETDATE() ) ) + '/' + | |
STR( DAY( GETDATE() ) ) | |
) | |
AS DATETIME | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CAST( | |
FLOOR( CAST( GETDATE() AS FLOAT ) ) | |
AS DATETIME | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
-- Get the full date/time stamp as a base. | |
( | |
GETDATE() | |
) AS date_time_part, | |
-- Trying casting to a string then back to a date. | |
( | |
CAST( | |
( | |
STR( YEAR( GETDATE() ) ) + '/' + | |
STR( MONTH( GETDATE() ) ) + '/' + | |
STR( DAY( GETDATE() ) ) | |
) | |
AS DATETIME | |
) | |
) AS date_only_part, | |
-- Try casting to float, rounding, and back to date. | |
( | |
CAST( | |
FLOOR( CAST( GETDATE() AS FLOAT ) ) | |
AS DATETIME | |
) | |
) AS date_only_part2, | |
-- Try casting just to float to see what it looks like. | |
( | |
CAST( GETDATE() AS FLOAT ) | |
) AS float_value, | |
-- Try flooring to see the intermediary step. | |
( | |
FLOOR( CAST( GETDATE() AS FLOAT ) ) | |
) AS int_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment