postgres sql to extract year-month - sql

Have a table with a column like this:
first_day_month
01/07/2020
01/07/2020
01/08/2020
01/09/2020
.......
Need to create a column like year-month,
Tried to_char(first_day_month, 'MM/YYYY') but got an error:
Error running query: INVALID_FUNCTION_ARGUMENT: Failed to tokenize string [M] at offset [0]
Tried
concat(extract(year from first_day_month),'-',extract(month from first_day_month) ) as month,
with an error:
Error running query: SYNTAX_ERROR: line 2:1: Unexpected parameters (bigint, varchar(1), bigint) for function concat. Expected: concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar)
Also tried date_parse but didn't get it right, any idea?
Thanks

You need to use TO_DATE first, to convert the column to a proper date. Then use TO_CHAR to format as you want:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'MM/YYYY') AS my
FROM yourTable;
Note that in this case since the text month year you want is actually just the right substring, you could also directly use RIGHT here:
SELECT RIGHT(first_day_month, 7)
FROM yourTable;
Finally, note that YYYY/MM would generally be a better format to use, as it sorts properly. So perhaps consider using this version:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'YYYY/MM') AS ym
FROM yourTable;

Your data doesn't seem to be of DATE type, might be string, then need to convert to DATE type first and format display style as desired pattern :
SELECT TO_CHAR(first_day_month::DATE,'MM/YYYY') AS first_day_month
FROM t
Demo

Related

How to get previous month from 'yyyy-MM' date format in Hive

I want to get previous month from 'yyyy-MM' format value in Hive. I tried with 'add_months' but couldn't able to get.
select add_months('2021-06', -1)
Eg: I have one string column having value '2021-06', now I want to display output as '2021-05'.
Please help on this.
Convert your string to date by concatenating with '-01', add_months, use date_format to get yyyy-MM:
select date_format(add_months(concat('2021-06','-01'), -1),'yyyy-MM')
Result:
2021-05
Another method (using substr instead of date_format):
select substr(add_months(concat('2021-06','-01'), -1),1,7) --returns 2021-05

Eliminate hours, minutes and seconds from a date in SQL ORACLE

I'm trying to remove the hours, minutes and seconds from a date using the following code :
TRUNC(column_date, 'YY')
But I get this : 01JAN2008:00:00:00, while I want this : 01JAN2008.
What should I do?
TRUNC() works as expected, and returns the original date truncated to year, as a date datatype. You want to format this date before displaying it (this actually means converting it to a string). For this, you can use the TO_CHAR() function.
You probably want:
TO_CHAR(TRUNC(column_date, 'YY'), 'ddmonyyyy')
Please note that this expression could be simplified to avoid the use of TRUNC(), as follows:
'01JAN' || TO_CHAR(column_date, 'yyyy')
I think you want to_char: https://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
Try using it in this way:
SELECT TO_CHAR(column_date, 'DD/MON/YYYY')
FROM x;
Where x is the table you are trying to query.
you only need to use
select to_char(column_date,'ddMONyyyy') FROM yourTable
Even you can invoke with
select to_char(column_date,'ddMONyyyy','nls_date_language=english') FROM yourTable
to guarantee to see the abbreviation JAN in the case your session/system date language value is different than english
to display as expected.

Error when using TO_DATE in SQL

I understand the basic TO_DATE usage in Oracle SQL. i google and found some guides to use TO_DATE which is to convert julien date to normal date.
the basic working code is :
SELECT TO_CHAR((TO_DATE(2016365, 'YYYYDDD'))) FROM DUAL)
However, i want to convert date that is in a column which has thousands of them.
What i did was :
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM DUAL)
The changes is PREVDT because all my julian date is in PREVDT column. However, im getting invalid identifier.....can anyone help me?
I also tried this but no luck:
TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD')))
You need to provide the table name in which your column PREVDT is present.
select TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM yourTable
Oracle DUAL table does not have the column PREVDT. What you want to do is run the query against your table, the one with column PREVDT.
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM your_table_name;
The PREVDT values should be in the specified date format otherwise you will get an error.
Read more about the DUAL table here.

SQL Convert String to Date

I am trying to find a way of extracting the first part of line of string and separating as a date. The following is an example of some of the data.
17/10/12 lskell Still waiting for one more signature on the
I have tried casting the whole field as a date, and converting to a date, but these fail?
Would anyone have any ideas?
Try this for MySql
DATE_FORMAT(STR_TO_DATE(SUBSTRING_INDEX(columnname,' ',1), '%d/%m/%y'), '%Y-%m-%d')
If you know that
the first "word" in your string will always be a date
the date will always be separated from the rest of the string by a space
the date will have a consistent format
you are working in MySQL
then try this:
SELECT CAST(SUBSTRING_INDEX(myfield, ' ', 1) AS DATE) adate FROM mytable;
Try:
SELECT SUBSTRING('10/17/12 lskell Still waiting for one more signature on the', 1, 8)
(assuming date comes in this format)
-- MYSQL:
SELECT Str_to_Date(Left(yourstring,8),'%d/%m/%y') from yourtable;
-- Oracle
SELECT TO_DATE(left(yourstring,8),'dd/mm/yy')
from your table;
--- sql server
SELECT CONVERT(DATETIME,left(yourstring,8),120) from your table;

Formatting date sql

I want to format existing dates in a column called mydate in a table called empl. However, I am going no where with this. I have tried
SELECT CONVERT(VARCHAR(10), mydate,103) from empl
but get a missing expression error.
I want to change the current format in the column which is in the format
dd-mm-yy to mm/dd/yy
EDIT: im using oracle
Any suggestions?
If the datatype of the column is DATE, you can use the TO_CHAR() function:
SELECT to_char(mydate, 'mm/dd/yy')
FROM empl ;
Are you able to change the column type to date? Then you could use date_format
The Convert statement you show is for MS-SQL, and you would want code 3 not 103, the 100 series give 4 digit years.
Try:
select to_char(mydate,'MM/DD/YY') from empl
Assuming mydate is stored in date datatype, and using oracle, easy way to do that is:
select to_char(mydate, 'dd/mm/yy') from empl
In Oracle, convert is used to change character set with parameters
(char, destination_char_set, source_char_set)
select TO_CHAR(sysdate, 'MM/DD/YYYY') CHRDATE from dual;