8

I have a record as '5/30/2016'. Now I want to have month and year from this date column. Output I want is as : '5/2016'. How can I do that in redshift?

1
  • Is '5/30/2016' stored as a string (as shown), or is it a date field and '5/30/2016' is merely how your SQL tool is displaying it? Commented Jul 13, 2017 at 12:30

1 Answer 1

13

select to_char('5/30/2016'::date,'MM/YYYY')

would give you '05/2016', and if you don't want the first zero you can trim it:

select ltrim(to_char('5/30/2016'::date,'MM/YYYY'),'0')

1
  • 1
    You can also use the DATE_TRUNC() function to round the value back. SELECT date_trunc('week', '2017-07-13'); = 2017-07-10 00:00:00
    – Joe Harris
    Commented Jul 13, 2017 at 16:58

Your Answer

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