0

Say I have a field numCommande with a string "1610131223ZVV40" where 161013 is a date in form yymmdd.

Is there any way in SQL to extract that 13/10/2016 date from the string field ?

Thanks!

4
  • 1
    Is it always the first six characters? No need for regular expressions if so, substr() and to_date() would be enough. What have you tried so far? Commented Mar 20, 2018 at 10:20
  • yes is always the first 6 caracters, thanks Commented Mar 20, 2018 at 10:23
  • Why is it 31/10/2016 not 13/10/2016? Commented Mar 20, 2018 at 10:24
  • it was an error .. thanks again Commented Mar 20, 2018 at 11:16

2 Answers 2

3

If the 'date' is always the first six characters, you can extract those with a plain substr() call:

substr(numCommande, 1, 6)

which gives you '161013'; and then convert that string to a date with a suitable format model:

select to_date(substr(numCommande, 1, 6), 'RRMMDD') from your_table;

Quick demo with static value instead:

select to_date(substr('1610131223ZVV40', 1, 6), 'RRMMDD') from dual;

TO_DATE(SU
----------
2016-10-13
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoting for use of RR over YY
1
SELECT TO_CHAR(TO_DATE(SUBSTR(Column_Name,1,6), 'YYMMDD'),'DD/MM/YYYY') FROM TableName

Live Demo

http://sqlfiddle.com/#!4/ce715/5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.