Postgresql - convert DATE into a specific DATE format - sql

I'm trying to convert the dates in my date column from:
yyyy-mm-dd
to
Month dd, yyyy
So far i've tried the below:
SELECT To_char(sq2.date_column, Month DD, YYYY)
but get the below error, despite my pattern aligning to the documentation
ERROR: syntax error at or near "DD"
I noticed in the documentation that To_char isn't explicitly referenced for date conversion (only time). Also, i'm not necessarily trying to alter the DATE type to VARCHAR. I would be happy to keep the data in the date_column as DATE type and just change the pattern.

You were damn close. Just missed the ' at the second parameter.
SELECT To_char('2018-01-20'::date, 'Month DD, YYYY')
Using your table column:
SELECT To_char(sq2.date_column, 'Month DD, YYYY')

you have to use single code before month and end of the YYYY.
you can use fallowing query for your required result
SELECT To_char(sq2.date_column, 'Month DD, YYYY')

Related

Convert YYY-MM-DD to MON DD, YYYY in PostgreSQL

I am having difficulty converting dates in a column from YYYY-MM-DD to Mon DD, YYYY
I think I first need to reorganize the dates and then use a case when statement to specify 01 = Jan and so on? Is that correct?
SELECT to_date(column_name, 'MM/DD/YYYY')
FROM table
gives me some incorrect dates
i.e. previous = 2012-01-29 and
result from query = 0197-06-26
Any suggestions? Thanks
I figured it out!
SELECT to_char(date(column_name), 'Mon dd, yyyy')
FROM table
gives me exactly what I need without the need of a case statement.

How to convert system date to YYYYMM format by using SQL Query?

I need to convert sysdate in YYYYMM format to validate the expiry date, this is the query I have written but am facing the below error.
select to_char (to_date(sysdate,'MM/DD/YYYY'),'YYYYMM') from dual;
ORA-01858: a non-numeric character was found where a numeric was expected
There is no reason to convert sysdate to a date. So just use:
select to_char(sysdate, 'YYYYMM')
If you wanna to format the date, try "DATE_FORMAT" way:
SELECT DATE_FORMAT('2011-11-11 13:14:01','%m%Y')
or you wanna to get the difference between the expired date and sysdate,you may try "DATEDIFF":
SELECT DATEDIFF('2001-01-01','2001-02-02') -> -32

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

Extract Month, Year (Netezza SQL)

My data type is a date formatted as "YYYY-MON-DD" and I would like to extract the month and year to be formatted as "MON YYYY" while keeping the data type as date so that I will be able to use it with the ADD_MONTHS function. Is there a way to do so? I extract the date from the data field called date_process.
This is what I thought of but it doesnt seem to be working.
SELECT TO_DATE(TO_CHAR(PROCESS_DATE,'YYYY-MON'), 'MON YYYY') AS PERIOD,
Thank you.
Dates are stored in an internal format, not as a string.
If you want to see the value in a particular format, then you need to convert it to a string. Hence, drop the final to_date():
SELECT TO_CHAR(PROCESS_DATE, 'MON YYYY') AS PERIOD,

issue with add_month date using PostgreSQL

I am trying to use the add_month function but getting an error. I want to get the number of visits between [CAL_DATE - 13 months] and [CAL_DATE]. The format of the dates are as following: 2007-14, 2010-05, 2009-04 and etc. this is the error I am getting
"Bad time stamp external representation '2009-11"
and here is the code I am using. I can't seem to figure out the issue.
CAL_DATE BETWEEN add_months(CAL_DATE,-13) AND CAL_DATE.
I am using netezza database.
Presumably add_months expects a date as its first argument and returns a date. You don't have dates, you have YYYY-MM strings so you have two problems:
add_months won't know what to do with a YYYY-MM string.
BETWEEN won't know what to do with a date and a YYYY-MM string.
If you want to use add_months then you'll have to give it a date and convert the date it gives you to one of your YYYY-MM strings with something like this:
to_char(add_months((cal_date || '-01')::date, -13), 'yyyy-mm')
Appending -01 to your strings should give you a string representation of the first of that month and you should be able to cast that to a date with ::date. Then a to_char to convert the result of add_months back to your YYYY-MM format.
Alternatively, since add_months isn't really doing anything useful for you here, just use a PostgreSQL interval for the month adjustment:
to_char((cal_date || '-01')::date - interval '13 months', 'yyyy-mm')