6 Ways to Convert a String to a Date/Time Value in SQL Server

If you need to convert a string into a date/time value in SQL Server, you have a number of options. In this post I outline six T-SQL functions that allow you to do this.

The six functions are:

  • CAST()
  • CONVERT()
  • PARSE()
  • TRY_CAST()
  • TRY_CONVERT()
  • TRY_PARSE()

Below are example of how you can use these functions to convert a string to a date/time data type.

Continue reading

How to Convert a String to a Date/Time in SQL Server using PARSE()

If you work with SQL Server, chances are you’ve used at least one of the CONVERT() or CAST() functions to convert from one data type to another. If you’ve ever encountered an error while trying to convert a string to a date/time data type, the PARSE() function could be what you need.

For example, if you have a string like say, Fri, 20 Jul 2018, the CONVERT() or CAST() functions will throw an error. But the PARSE() function will handle it without a problem.

The PARSE() function returns the result of an expression, translated to the requested data type in SQL Server. So you can use it to “translate” your string value into a date/time data type (such as date, datetime, datetime2, etc).

Continue reading

How to Convert a Date/Time Value to a String in SQL Server using CONVERT()

When using SQL Server, you can convert a date/time value into a string by using the CONVERT() function. This function allows you to convert between different data types.

In this article, we’ll be converting between various date/time data types to a varchar or nvarchar string.

One of the good things about this function is that it allows you to specify the style that the date will be returned in. For example, you can specify whether it’s returned as mm/dd/yyyy, yyyy.mm.dd, Mon dd, yyyy, etc. You can also specify whether the time component is returned and how it’s styled.

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

How to Convert a String to a Date/Time in SQL Server using CAST()

In SQL Server, you can use the CAST() function to convert an expression of one data type to another.  This function works almost the same as the CONVERT() function, except that the syntax is slightly different (and CAST() doesn’t accept the style argument).

So if you need to convert a string to a date/time value, you can use the CONVERT() function or the CAST() function.

And if you get an error while using those two functions, the PARSE() function might be just what you need.

This article contains examples using the CAST() function.

Continue reading

How to Find the Last Day of the Month in SQL Server

Starting with SQL Server 2012, the EOMONTH() function allows you to find the last day of any given month. It accepts two arguments; one for the start date, and one optional argument to specify how many months to add to that date.

This article provides examples that demonstrate how EOMONTH() works in SQL Server.

Continue reading

6 Functions to Get the Day, Month, and Year from a Date in SQL Server

Transact-SQL includes a bunch of functions that help us work with dates and times. One of the more common tasks when working with dates is to extract the different parts of the date. For example, sometimes we only want the year, or the month. Other times we might want the day of the week. Either way, there are plenty of ways to do this in SQL Server.

In particular, the following functions allow you to return the day, month, and year from a date in SQL Server.

These functions are explained below.

Continue reading