Home > Enterprise >  How can I convert a date that's in "yyyy-mm/dd" to "yyyy-mm-dd"
How can I convert a date that's in "yyyy-mm/dd" to "yyyy-mm-dd"

Time:11-03

I have a column that's called appt_date (varchar(50)) that's in the format of YYYY-MM/DD and I want to convert it to YYYY-MM-DD

I've tried several replace, convert, cast functions and solutions found on here but I still end up with the same result.

I greatly appreciate the help in advance.

CodePudding user response:

I will strongly suggest to change your data base design and never store data in text format.

You can try and let me know if it helps:

  CREATE TABLE test_tbl (
  appt_date varchar(50) );

  insert into test_tbl 
  values ('2021-09/12'),
         ('2021-11/01'),
         ('2020-07/06');

 SELECT TRY_PARSE(appt_date as date ) as appt_date 
 FROM test_tbl;

Demo: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b8a3b59b48aa51b5ff2eedccf8f12c15

More info on: https://www.sqlshack.com/sql-server-functions-for-converting-string-to-date/

  • Related