Search code examples
sqlsql-serverdateconverterspatindex

How to convert a date 15-May-2019 in SQL to 2019/05/15 using PATINDEX


I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.

This is the code I have so far

CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)

I'm not completely sure how to use Patindex. Can someone please help me to fix this?


Solution

  • Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/

    SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)