Convert date to specific format using Postgresql - sql

I am needing to convert records where the TEXT values look like this using Postgresql:
26-AUG-2015
to:
2015-08-26
I'm not sure what version of Postgresql exists on the vendor server but I tried to do a select statement using:
SELECT to_char(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entires sle;
But I'm getting this error:
Error: SQL Error: SQLSTATE[42883]: Undefined function: 7 ERROR: function to_char(text, unknown) does not exist LINE 25: AND to_char(sle.log_field1, 'YYYY-MM-DD') >=... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I did also try:
SELECT to_date(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entries sle
But I got this error:
Error: SQL Error: SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid value "[E] " for "YYYY" DETAIL: Value must be an integer. Query: SELECT to_date(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entries sle
Any suggestions/direction would be appreciated. Thanks.

This assumes that lc_time is set to English:
SELECT to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD');
to_char
------------
2015-08-26
(1 row)

You can convert the value to a date and then back to a string:
select to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
Under many circumstances, processing the value as a date is probably sufficient.
Your approach doesn't work because the value is apparently already stored as a string, so converting it back to a string with a date format doesn't make sense.
EDIT:
You may be able to get by using a simple regular expression:
select (case when col ~ '^[0-9]{2}[-][A-Z]{3}[-][0-9]{4}$'
then to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
end)
Of course, if the formatting errors are more subtle, then a more complex regular expression would be needed.

try this,
SELECT to_char(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entires sale;

Related

Getting a date format error while executing

ORA-01840: input value not long enough for date format
01840. 00000 - "input value not long enough for date format"
*Cause:
*Action:
SELECT TO_DATE (
TO_CHAR (TO_DATE (attribute39, 'MM/DD/YYYY'), 'DD/MM/YYYY'),
'DD/MM/YYYY') AS "PO Valid To Date"
FROM table;
Want to execute the query without error,
attribute39 is date formate in mm/dd/yyyy and varchar(250)
TO_DATE supports 'DEFAULT return_value ON CONVERSION ERROR' and if you prefix the date format with 'FX' you will be able to detect rows where attribute39 is not exactly compliant to your expectation:
TO_DATE (attribute39, DEFAULT to_date('01/01/0001','dd/mm/yyyy') ON CONVERSION ERROR, 'FXMM/DD/YYYY')
You could put NULL as DEFAULT if you don't have it as possible value for attribute39, if not selecting a value you are sure is not in your data makes easier to detect rows with invalid attribute39.
You may get ORA-01840 if you have strings where the year is only two digits (meaning from 1950 to 2049).
You could also run a query with a regex to detect unexpected values in attribute39.
select distinct(col1)
from Customer_flex_attr_value
where regexp_like (col1, '([0-9][0-9]|3[0-1])/([0-9]|[0-9]{2})/[0-9]{4}')
order by 1 desc;
This would bring different date format

postgres sql to extract year-month

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

format date in oracle SQL

I was trying to learn how to format date in oracle pl oracle, when I ran below query its returns error
SELECT TO_DATE('01-JAN-00', 'YYYY-DD-MM') FROM dual;
the error message is
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
You are either not using the correct format specifier, or not passing the correct string. You want:
SELECT TO_DATE('2000-01-01', 'YYYY-DD-MM') FROM DUAL;
Or:
SELECT TO_DATE('01-JAN-00', 'DD-MON-YY') FROM DUAL;
Or you can simply declare a DATE litteral:
SELECT DATE'2000-01-01' FROM DUAL;
for my scenario I had to use to_char which perfectly solve the formatting issue.
SELECT TO_CHAR('01-JAN-00', 'yyyy-DD-MM') FROM dual;

ORA-00920: invalid relational operator

In a database, I am trying to pull information that is later than a specified date. I should note beforehand that the date is in an odd format: YYYYMMDDHH24MISS## where ## is a two letter string which defines something useless to my query. Thus, I am using substr to just remove them.
My query, below, throws the following error, and I canot find out why:
[Error Code: 920, SQL State: 42000] ORA-00920: invalid relational
operator
My Query:
SELECT *
FROM table_name
WHERE to_date(substr(COLUMN_NAME,1,14), 'YYYYMMDDHH24MISS')) >=
to_date('MIN_DATE', 'YYYYMMDDHH24MISS')
I have checked to make sure the dates are being defined correctly, and they are.
Example of what I have used for MIN_DATE is: 20140101000000
You have an extra parenthesis at the end of the first to_date
You get this error in Oracle when you are missing a comparison operation, such as = -- as John Maillet already noted.
My concern is the second part of the where clause:
where to_date(substr(COLUMN_NAME, 1, 14), 'YYYYMMDDHH24MISS') >=
to_date('MIN_DATE', 'YYYYMMDDHH24MISS')
You have MIN_DATE in single quotes. This is interpreted as a string with eight letters in it, starting with 'M' and ending with 'E'. This is not interpreted as a variable. Presumably you mean:
where to_date(substr(COLUMN_NAME, 1, 14), 'YYYYMMDDHH24MISS') >=
to_date(MIN_DATE, 'YYYYMMDDHH24MISS')
You should only use single quotes for string and date constants.
I should add that you should be able to do this comparison without having to convert to dates:
where left(COLUMN_NAME, 14) = MIN_DATE

Comparing date with sysdate in oracle

I have a column which is of "DATE" type and I want to run a query on it comparing it with sysdate.
But I am getting following error, Can someone please let me know what I am missing here?
SQL> select distinct file_name as r
from table_1
where view_day >= TO_DATE(SYSDATE-10, 'YYYY/MM/DD');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
You shouldn't use to_date on a date, To_date is for casting a varchar to date, not a date.
If you do use the function to_date on a date, then oracle will refer to it as a string according to nls_date_format which may vary in different environments.
As #jonearles said, if you want to remove the time in sysdate then use TRUNC
USE:
select distinct file_name as r
from table_1
where view_day >= TRUNC(SYSDATE-10)
Error shows that a VIEW_DAY column is varchar so you need to convert DATE to String. Use TO_CHAR or convert VIEW_DAY to date type.