1

I have a very simple tag in a table, structured as tag-DDMMYY. I want to convert that into a date, MM-DD-20YY. Is there an easy way to do this in SQL?

I'm not an expert developer and I feel like this should be quite easy using PatIndex (there's always only one dash), but I've got nowhere so far and I'm hoping someone expert could help.

Emilio
  • 11
  • 1

3 Answers3

0

This is mysql

You can use some date and string function

select  str_to_date(substr(my_column, LOCATE('-', my_column), 50),%d%m%y)
from you_table ;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

In MSSQL, you can do something like:

SELECT SUBSTRING(T1.OriginalDate, PATINDEX('%-%', T1.OriginalDate)+3, 2) 
     + '-' 
     + SUBSTRING(T1.OriginalDate, PATINDEX('%-%', T1.OriginalDate)+1, 2) 
     + '-' 
     + '20' + SUBSTRING(T1.OriginalDate, PATINDEX('%-%', T1.OriginalDate)+5, 2) 
     AS NewFormatDate
FROM (SELECT 'tag-310516' as OriginalDate) T1
Matt Roy
  • 1,455
  • 1
  • 17
  • 27
0

Try this..(SQL Server)

  select cast (stuff(stuff(substring(column,patindex('%-%',column)+1,len(column)),5,0,'.20'),3,0,'.') as date) 
   from mytable

Borrowed from here

For mysql you can try something like this

select str_to_date(concat(substring(yourcolumn,instr(yourcolumn,'-')+1,4),'20',substring(yourcolumn,length(yourcolumn)-1,2)),'%d%m%Y')
from yourtable
Community
  • 1
  • 1
cableload
  • 4,215
  • 5
  • 36
  • 62