CONVERT() from Date/Time to String Examples in SQL Server

The CONVERT() function allows you to convert between data types. It’s similar to the CAST() function, but one of the benefits of CONVERT() is that, when you convert from a date/time data type to a string, you can add an optional argument that specifies the style that you want the return value to be in. For example, you can have it returned as dd.mm.yyyy, yyyy-mm-dd, dd mon yyyy, etc

This article contains examples of the various styles you can return when converting a date/time value to a string using the CONVERT() function in SQL Server.

Continue reading

How to Convert Between Date Formats in SQL Server using CAST()

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). However, that’s not the only function in the T-SQL toolbox for converting between data types.

The CAST() function is part of the ANSI SQL standard, and it does most of the things CONVERT() does. So in many cases, you have the option of which of these functions you prefer to use.

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).

In any case, this article provides examples of converting between different date formats using CAST().

Continue reading

How to Convert from One Date Format to Another in SQL Server using CONVERT()

Sometimes when working with databases and other programming environments, you get a date value but it’s in the wrong format/data type. For example, if a date has been generated with an inbuilt date function, it might include both the date and the time, right down to the last nanosecond. And all you want is the day, month, and year, say like this: 2018-01-01.

If this happens while you’re using SQL Server, you can use the CONVERT() function to convert it to another data type. When you do this, the data type will determine the format it appears as.

This article provides examples of using the CONVERT() function in SQL Server to convert a date value to another (date) data type.

Continue reading

CONVERT() vs TRY_CONVERT in SQL Server: What’s the Difference?

You might have noticed that T-SQL includes both a CONVERT() function and a TRY_CONVERT() function that you can use in SQL Server to perform conversions between data types. But if you’re scratching your head over what the difference is between these two functions, read on!

The difference between CONVERT() and TRY_CONVERT() is in the way they handle data types that can’t be converted. One throws an error, while the other returns null. The following examples demonstrate this.

Continue reading

CAST() vs TRY_CAST() in SQL Server: What’s the Difference?

When converting between data types in SQL Server, you will run into various functions that seemingly do the same thing. However, there are usually differences between these functions that might not be apparent at first glance. An example of this is the difference between the CAST() and TRY_CAST() functions.

This article demonstrates the difference between these functions when using SQL Server.

Continue reading