Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:15
Show Gist options
  • Save bennadel/9751387 to your computer and use it in GitHub Desktop.
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
CAST(
(
STR( YEAR( GETDATE() ) ) + '/' +
STR( MONTH( GETDATE() ) ) + '/' +
STR( DAY( GETDATE() ) )
)
AS DATETIME
)
CAST(
FLOOR( CAST( GETDATE() AS FLOAT ) )
AS DATETIME
)
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