0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luna
  • 3
  • 1

3 Answers3

2

I would recommend just converting to the date data type:

select try_convert(date, '15-May-2019')

If you want it with slashes, you can produce a string instead:

select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

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)
Nats N
  • 41
  • 1
0

PATINDEX is slightly overkilling.

Try:

SELECT FORMAT(
      TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
  • TRY_PARSE parses the string to DATE using English culture.
  • FORMAT is to precisely control the output
Alexander Volok
  • 5,630
  • 3
  • 17
  • 33