How do I convert string to a date with SQL?

Hit a couple of error messages when trying to convert a string to a date?

And that’s totally not surprising!

As you’ll see below the text-to-date conversion functions within the different database environments seldom have identical syntax

-----------------------Vertica--------------------------------
select
'2017-03-31' as TextField,
to_date('2017-03-31', 'YYYY-MM-DD') as Datefield
-----------------------PostgreSQL----------------------------
select
'2017-03-31' as TextField,
to_date('2017-03-31', 'YYYY-MM-DD') as Datefield
------------------------MySQL---------------------------------
select
'2017-03-31' as TextField,
str_to_date('2017-03-31', '%Y-%m-%d') as DateField
-----------------------SQL Server-----------------------------
select
'2017-03-31' as TextField,
convert(date, '2017-03-31') as DateField1,
cast('2017-03-31' as date) as DateField2

Why would you want to convert a string to a date?

Well, it could be pretty useful if you want to:

  • return the end date of a given quarter
  • calculate the remaining days until the end of the current calendar quarter
  • calculate the remaining days until the end of the current fiscal quarter

Wanna know how to do these?