POSTGRESQL: Error: date format not recognized - sql

Have a table "user_order" with columns "user_id", "code" (character varying) and created_date_key (integer). I am trying to write a query which displays all records for code 26 and date greater that '12-5-2013 23:59:59'.
Select *
from user_order
where code like 26 ::text
and to_date(created_date_key ::text, 'YYYY-MM-DD') > to_date ('12-5-2013 23:59:59' ::text, 'YYYY-MM-DD')
ERROR: date format not recognized.

created_date_key should be timestamp and not integer.

select *
from user_order
where code like 26 ::text
and to_date(created_date_key ::text, 'YYYY-MM-DD') > '12-5-2013 23:59:59'

String values need to be included within single quotes. You do not need to specify text to convert the data type. to_timestamp function is used to convert the text data to timestamp value. The following query would select the required data:
Select *
from user_order
where code = '26'
and created_date_key > to_timestamp ('12-5-2013 23:59:59', 'MM-DD-YYYY HH24:MI:SS');
References:
SELECT on PostgreSQL Manual. There are a lot of examples on this page.
Data Type Formatting Functions on PostgreSQL Manual

Related

Not a Valid Month - Working with Dates in Oracle

I am finding it strenuous to work with dates in my customized environment. I have a request to add a where clause which caters to specific dates but I just cannot get oracle to budge. Any ideas anyone please.
select created_date, cast(created_date as date) as created_date_cast
from mytable;
created_date created_date_cast
04-Mar-20 05.21.15.772000 AM 3/4/2020 5:21:15 AM
04-Mar-20 05.21.15.709000 AM 3/4/2020 5:21:15 AM
04-Mar-20 05.17.14.902000 AM 3/4/2020 5:14:14 AM
28-Feb-20 01.15.25.702700 AM 2/28/2020 1:15:25 AM
When I try to add a where clause the snippet blows up with the error:
select created_date, cast(created_date as date) as created_date_cast
from mytable
where cast(created_date as date) <= '02/28/2020';
ORA-01843: not a valid month
I have also tried to_date(created_date, 'MM/DD/YYYY') in the from but proves to be erroneous with:
ORA-01858: a non-numeric character was found where a numeric was expected
Firstly cast as date which converts a timestamp value to a date value, and then don't forget to add trunc() function in order to include the boundry value (date'2020-02-28' in this case) also as
where trunc(cast(created_date as date)) <= date'2020-02-28'
Demo
Don't use CAST and don't use TRUNC (as then Oracle will not be able to use an index on your column but would, instead, require a function-based index created on TRUNC(created_date)) just add a day and use a literal:
SELECT created_date
FROM mytable
WHERE created_date < DATE '2020-02-29';
or
SELECT created_date
FROM mytable
WHERE created_date < TIMESTAMP '2020-02-29 00:00:00';
or, if you want to specify the exact date then just add a day. E.g.:
SELECT created_date
FROM mytable
WHERE created_date < DATE '2020-02-28' + INTERVAL '1' DAY;
All of those options should be able to use an index on the created_date column.
I have also tried to_date(created_date, 'MM/DD/YYYY') in the from but proves to be erroneous with:
ORA-01858: a non-numeric character was found where a numeric was expected
TO_DATE( value_string, format_model ) takes strings as its arguments but CREATED_DATE is a TIMESTAMP data type and not a string so Oracle must make an implicit TIMESTAMP-to-string conversion and it does this using the NLS_TIMESTAMP_FORMAT session parameter; so your expression is effectively:
TO_DATE(
TO_CHAR(
created_date,
( SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_TIMESTAMP_FORMAT' )
),
'MM/DD/YYYY'
)
And if your NLS_TIMESTAMP_FORMAT is not MM/DD/YYYY then its highly likely that an exception will be raised (i.e. like the ORA-01858 you had).
You should never rely on implicit string conversions as any user can change their own session parameters at any time and an implicit conversion that works for one user may not work for another just because they have different parameter values (even though the queries are identical).
Use a date literal:
cast(created_date as date) <= date '2020-02-28'
I would also recommend dispensing with the cast() -- assuming that created_date is correctly stored as a date or timestamp:
created_date < (date '2020-02-28' + interval '1' day)

Timestamp to date convertion in oracle sql [duplicate]

How can we convert timestamp to date?
The table has a field, start_ts which is of the timestamp format:
'05/13/2016 4:58:11.123456 PM'
I need to query the table and find the maximum and min timestamp in the table but I'm not able to.
Select max(start_ts)
from db
where cast(start_ts as date) = '13-may-2016'
But the query is not returning any values.
Please help me in finding the max timestamp for a date.
CAST(timestamp_expression AS DATE)
For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;
Try using TRUNC and TO_DATE instead
WHERE
TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD')
Alternatively, you can use >= and < instead to avoid use of function in the start_ts column:
WHERE
start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD')
Format like this while selecting:
to_char(systimestamp, 'DD-MON-YYYY')
Eg:
select to_char(systimestamp, 'DD-MON-YYYY') from dual;
If the datatype is timestamp then the visible format is irrelevant.
You should avoid converting the data to date or use of to_char. Instead compare the timestamp data to timestamp values using TO_TIMESTAMP()
WHERE start_ts >= TO_TIMESTAMP('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_TIMESTAMP('2016-05-14', 'YYYY-MM-DD')
You can try the simple one
select to_date('2020-07-08T15:30:42Z','yyyy-mm-dd"T"hh24:mi:ss"Z"') from dual;
You can use:
select to_date(to_char(date_field,'dd/mm/yyyy')) from table
I'd go with the following:
Select max(start_ts)
from db
where trunc(start_ts) = date'13-may-2016'
If you have milliseconds in the date string, you can use the following.
select TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')From Dual;
And then convert it to Date with:
select trunc(TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')) From Dual;
It worked for me, hope it will help you as well.
This may not be the correct way to do it. But I have solved the problem using substring function.
Select max(start_ts), min(start_ts)from db where SUBSTR(start_ts, 0,9) ='13-may-2016'
using this I was able to retrieve the max and min timestamp.

Oracle Data Conversion: ORA-01722: invalid number

Error while converting varchar2 column value to Number
i am unable to compare varchar2 value field with another value
like this '10:54' with '11.00', i have to compare minutes with minutes field.
How is it possible?
Query is:
select
adm_emp_num,adm_emp_nam,adm_ace_dte,asm_sft,asm_ed_tme,asm_st_tme,adm_ace_tme from Adm,asm
where adm_ace_dte =ass_sft_dte
and to_char(adm_ace_dte,'YYYYMM')=201409
--having min(to_char(adm_ace_tme,'HH24:MI')) < asm_st_tme
having min(to_char(adm_ace_tme,'HH24:MI')) < to_char(to_number(asm_st_tme,'99999
9.99'),'999999.99')
group by adm_emp_num,adm_emp_nam,adm_ace_dte,asm_sft,asm_ed_tme,asm_
st_tme,adm_ace_tme
order by 1,2
refer the table structures in the link:
https://stackoverflow.com/review/suggested-edits/5861972
Any help will be appreciated
Thanks
Use EXTRATC function to get minute part. Examples:
from TIMESTAMP type:
SELECT EXTRACT(MINUTE FROM SYSTIMESTAMP) FROM DUAL;
from DATE type:
SELECT EXTRACT(MINUTE FROM CAST(SYSDATE AS TIMESTAMP)) FROM DUAL;
from VARCHAR2 type (for example: 11:30):
SELECT EXTRACT(MINUTE FROM TO_TIMESTAMP('11:30', 'HH24:MI')) FROM DUAL;
You can do it like this:
select to_number(to_char(to_date('12:56', 'hh24:mi'), 'mi')) from dual;
i.e. first convert your string to a valid date, then extract the minute part (by specifying just 'mi' in the format), then casting that to a number.
You can then perform comparisons:
...where to_number(to_char(to_date('12:56', 'hh24:mi'), 'mi')) = ...
You could of course just parse out the substring, but you would then be vulnerable to invalid minutes, like 79.
You can substring your value and convert it to number, for example:
...
WHERE TO_NUMBER(SUBSTR('10:54',4,2)) >= TO_NUMBER(SUBSTR('11:00',4,2));
--for minutes
...
WHERE TO_NUMBER(SUBSTR('10:54',1,2)) >= TO_NUMBER(SUBSTR('11:00',1,2));
--for hours

YYYYMMDD to YYYYMM in oracle

I have a column with DATE datatype in a table.
I am trying to retrieve the column values in YYYYMM format. My select query looks like below
select *
from tablename
where date column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I am getting below exception.
ORA-01847: day of month must be between 1 and last day of month
Appreciate any input on this.
I think the simplest method is:
where to_char(datecolumn, 'YYYYMM') = '400012'
Or, if you prefer:
where to_char(datecolumn, 'YYYYMM') = to_char(to_date('12/31/4000', 'MM/DD/YYYY'), 'YYYYMM');
Syntax-wise, the right hand date (to the right of the equals) is OK. But you are doing a character comparison, not a date comparison.
This works for me in multiple databases:
select to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM')
from dual;
Even though your column is named DATE_COLUMN, you are comparing based on characters in the query.
So, try this instead - this compares based on dates (NOT a character comparison) and truncates off the hour, minute, ETC. so you are only comparing the DAY:
select * from DATE_TAB
where TRUNC(DATE1, 'DDD') = TRUNC(to_date('12/31/4000','MM/DD/YYYY'),'DDD');
NOTE: The DATE1 field above is a DATE field. If you're DATE_COLUMN is not a DATE field, you must
convert it to a DATE datatype first (using TO_DATE, ETC.)
Assuming that "date_column" is actually a date, and that you have an index on date_column, you can do something like this to return the data quickly (without truncating dates in all rows to do a comparison):
with dat as (
select level as id, sysdate - (level*10) as date_column
from dual
connect by level <= 100
)
select id, date_column
from dat
where date_column between to_date('11/1/2013', 'MM/DD/YYYY') and last_day(to_date('11/2013 23:59:59', 'MM/YYYY HH24:MI:SS'))
Here I just dummy up some data with dates going back a few years. This example picks all rows that have a date in the month of November 2013.
If your date_column's data-type is DATE, then use
select *
from tablename
where TO_CHAR(date_column,'YYYYMM') = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your date_column's data-type is VARCHAR, then use:
select *
from tablename
where date_column = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I somehow feel your error is because you have a space between date and column as
"date column". If the field name in the table is "COLUMN", then just removing the word "DATE" from your original query would suffice, as:
select *
from tablename
where column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your column (YYYYMMDD) is in number format, the simplest way to get YYYYMM would be
select floor(DATE/100)
from tablename;

How to format bigint field into a date in Postgresql?

I have a table with a field of type bigint. This field store a timestamp.
I want to date format the field like this :
to_char( bigint_field,'DD/MM/YYYY HH24:MI:SS')
I get the following error :
ERROR: multiple decimal points
État SQL :42601
TO_CHAR(TO_TIMESTAMP(bigint_field / 1000), 'DD/MM/YYYY HH24:MI:SS')
This is what worked for me
to_timestamp( bigint_field/1000)::date
This depends on what the bigint value represents - offset of epoch time, or not.
select to_timestamp(20120822193532::text, 'YYYYMMDDHH24MISS')
returns
"2012-08-22 19:35:32+00"
I did it like this:
to_timestamp(to_char(20120822193532, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS')
the result looks like this:
2012-08-22 19:35:32
you also can use this in you select statemant, just exchange the number with your database colunm.
Step by Step Explanation:
to_char(20120822193532, '9999-99-99 99:99:99')
This will create a string like this:
"2012-08-22 19:35:32"
now we can easiely convert this into a timestamp:
to_timestamp('2012-08-22 19:35:32','YYYY-MM-DD HH24:MI:SS')
Result will look the same as before, but it's now a timestamp.
Also, if you use this for a command like
CREATE TABLE table2 AS SELECT to_timestamp(to_char(tb1.date, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS') AS realDate FROM table1 AS tb1;
you might end up with timstamptz (timestamp with time zone) instead of timestamp (timestamp without time zone). You can change it like this:
ALTER TABLE table2 ALTER realDate SET DATA TYPE timestamp USING realDate;