SELECTĬAST(SYSDATETIME() AS datetimeoffset) AS Converted In this example, we cast the date as a datetimeoffset data type. SELECTĬAST(SYSDATETIME() AS smalldatetime) AS Converted Įxample 3 – Cast SYSDATETIME() as datetimeoffset In this example, we cast the date as a smalldatetime data type. +-+-+Įxample 2 – Cast SYSDATETIME() as smalldatetime SELECTĬAST(SYSDATETIME() AS date) AS Converted Note that the SYSDATETIME() generates its value as a datetime2(7) data type, so we are converting it from that data type to another data type. In this example, we generate the current date/time with the SYSDATETIME() function and cast the return value as a date data type. length An optional integer that specifies the length of the target data type. This includes xml, bigint, and sql_variant. These arguments are defined as follows: expression Any valid expression. The syntax goes like this: CAST ( expression AS data_type ) In any case, this article provides examples of converting between different date formats using CAST(). Many database professionals prefer CAST() due to the fact that it’s part of the ANSI SQL standard, however, others prefer CONVERT() due to the extra functionality that T-SQL’s implementation offers (such as being able to provide a date style). So in many cases, you have the option of which of these functions you prefer to use. The CAST() function is part of the ANSI SQL standard, and it does most of the things CONVERT() does. However, that’s not the only function in the T-SQL toolbox for converting between data types. In SQL Server, you can use use the CONVERT() function to convert a date value from one date data type to another (or between any other data type for that matter).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |