DATETIME to DATE and TIME

SELECT GETDATE(), CONVERT(DATE, GETDATE()), CONVERT(TIME(0), GETDATE()), CAST(GETDATE() AS DATE)

Should I use CAST or Convert?

Unless you have some specific formatting requirements you’re trying to address during the conversion, I would stick with using the CAST function.  There are several reason I can think of:

  1. CAST is ANSI-SQL compliant; therefore, more apt to be used in other database implementation.
  2. There is no performance penalty using CAST.
  3. I think CAST is easier to read, and since it is part of the ANSI specification, your non-SQLServer DBA think so too!

Sources:

https://www.essentialsql.com/what-is-the-difference-between-cast-and-convert/

Last modified: January 6, 2020

Author

Comments

Write a Reply or Comment