Date Conversion Function PostgreSQL - postgresql

I have the following function:
CREATE function SEMANA_ISO (fecha date) returns text as $$
select to_char(fecha, 'mm-dd-yyyy');
$$ LANGUAGE sql;
It works with:
Select SEMANA_ISO ('28/12/2014');
Select SEMANA_ISO ('01/01/2015');
Select SEMANA_ISO ('01/07/2015');
As you may see below
But not with:
Select SEMANA_ISO ('12/31/2014');
It shows:
********** Error **********
ERROR: The value of time / date is out of range "12/31/2014"
SQL state: 22008
Hint: You may need a different configuration of "dateStyle".
Character: 20
Do you have any suggestion without having to change the datestyle so I can enter
Select SEMANA_ISO ('12/31/2014');
And get an output of:
12-31-2014
using just one function to "parse" all dates?

Your function is declared to get a parameter of type date so you also need to pass such a value. '12/31/2014' is a character value, not a date.
When you pass a character literal (aka string) to the function Postgres is forced to do an implicit data type conversion based on the current datestyle - not something you should rely on.
If you want to call the function independently of the datestyle you need to pass a proper date literal, e.g. DATE '2014-12-31'
For more details on specifying date values, please see the manual: http://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-DATETIME-INPUT

If you want 12/31/2014 accepted as a date set datestyle to iso, dmy;
if you want 31/12/2014 accepted as a date set datestyle to iso, mdy;
you can't have both at the same time, else 4/1/2015 is ambiguos, and must be rejected.

There is to_date() to input date literals with an arbitrary (given) format:
SELECT to_date('12/31/2014', 'MM/DD/YYYY') AS date1
, to_date('31/12/2014', 'DD/MM/YYYY') AS date2;
That's "without changing any datestyle". Obviously, you need to provide a matching format pattern, though.
To output the same in any desired format, use to_char():
SELECT to_char(to_date('12/31/2014', 'MM/DD/YYYY'), 'DD/MM/YYYY') AS date_as_text

Related

Looking for solution to swap position of date format DMY to YMD

I Have some difficulties with my data base.
i have uploaded data from multiple excel file, each file has a spécific date Format. some time DD/MM/YYYY and some time YYYY/MM/DD the column is character varring.
i want to make them YYYY/MM/DD.
A simple solution:
select regexp_replace('05/01/2019', '(\d\d)/(\d\d)/(\d\d\d\d)', '\3/\2/\1')
regexp_replace
----------------
2019/01/05
(1 row)
You could update the table with
update my_table
set date_column = regexp_replace(date_column, '(\d\d)/(\d\d)/(\d\d\d\d)', '\3/\2/\1')
However, you should basically store dates in a column of type date. Use the function to convert differently formatted texts to dates:
create or replace function iso_date(text)
returns date language sql immutable as $$
select case
when $1 like '__/__/____' then to_date($1, 'DD/MM/YYYY')
when $1 like '____/__/__' then to_date($1, 'YYYY/MM/DD')
end
$$
The above is an example, you can modify the function if you have more different formats. Now you can alter the column type in this way:
alter table my_table alter date_column type date using iso_date(date_column);
Read more about Data Type Formatting Functions and POSIX Regular Expressions in the documentation.

Impala Date to String Conversion

How to check the type of a column in impala? Query is
select TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'))
Also I want to format the result of the query as a string yyyyMMdd. CAST returns null. Any tips?
Could you try below query in impala.
Case1:-
select cast(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd')) as string);
Result
2018-03-16
Case2:-
select cast((FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyyMMdd')) as string);
Result
20180316
Case3:-
select cast(regexp_replace(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss')),'-','')as string);
Result
20180316
You can use regexp_replace to replace all the - with empty string so that we can have yyyyMMdd format.
I don't get exactly your question on "check the type of a column". TO_DATE function is returning the result as string.
For your 2nd question, you can use current_date to get the server date then use a function called "DATE_FORMAT". By the way, Unix timestamp is useful if you want up to hours, mins, seconds and micro seconds and current_date function is sufficient if there is no time required.
SELECT DATE_FORMAT(CURRENT_DATE, 'yyyyMMdd');
Result: 20180316
Hi use the following script
select from_timestamp('2020-01-01','yyyy-MM-dd')

Oracle - Get date from date time using to_date()

I getting an XML date/time from an XML file and need to load it in a datatype of just Date, so I have to truncate the time.
I would like to try something like this in order to let Oracle do the truncation:
TO_DATE('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD')
I verify this failed by running this:
SELECT TO_DATE('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD') FROM DUAL
It throws error: ORA-01830: data format picture ends before converting entire string.
I'm trying to minimize change to my C# program that is building the SQL statements. If I need to, I can change my C# code to generate this:
TO_DATE('2015-11-04','YYYY-MM-DD')
Can Oracle handle the truncation or must I do it in my program?
If you're storing your information in a DATE column in Oracle (which accepts date-and-time), then the following should help you see what you need to do:
select to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm') tz,
to_date(to_char(to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm'), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') tz_char_date,
cast(to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm') as date) tz_cast_date
from dual;
TZ TZ_CHAR_DATE TZ_CAST_DATE
---------------------------------------- ------------------- ---------------------
04/11/2015 13:45:19.387000000 -05:00 04/11/2015 13:45:19 2015-11-04 13:45:19
An alternative is to use substr to shorten the string to get the portion you're interested in:
select to_date(substr('2015-11-04 13:45:19.387-05:00', 1, 19), 'yyyy-mm-dd hh24:mi:ss') substr_date
from dual;
SUBSTR_DATE
---------------------
2015-11-04 13:45:19
You can use the to_timestamp_tz() function to convert the string from XML into a timestamp with timezone value:
SELECT TO_TIMESTAMP_TZ('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD HH24:MI:SS.FFTZH:TZM')
FROM DUAL;
TO_TIMESTAMP_TZ('2015-11-0413:45:19.387-05:00','YYYY-MM-DDHH24:MI:SS.FFTZH:TZM')
--------------------------------------------------------------------------------
04-NOV-15 13.45.19.387000000 -05:00
You can then use the trunc() function to truncate the time portion to midnight; this also converts it implicitly from a timestamp to a date:
SELECT TRUNC(TO_TIMESTAMP_TZ('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD HH24:MI:SS.FFTZH:TZM'))
FROM DUAL;
TRUNC(TO_TIMESTAMP_TZ('2015-11-0413:45:19.387-05:00','YYYY-MM-DDHH24:MI:SS.FFTZH
--------------------------------------------------------------------------------
04-NOV-15
This ignores the actual time zone - essentially assuming the values are in your system timezone (i.e. you are in the same -05:00 region).
You could also use a substring to strip the time and timezone part from the raw string before converting:
SELECT TO_DATE(SUBSTR('2015-11-04 13:45:19.387-05:00', 1, 10), 'YYYY-MM-DD')
FROM DUAL;
TO_DATE(SUBSTR('2015-11-0413:45:19.387-05:00',1,10),'YYYY-MM-DD')
-----------------------------------------------------------------
04-NOV-15
... or performing the substring in C# if you prefer, assuming that is parsing the XML document.
(These are intentionally displaying in a format that is different from the ISO standard so they don't look like the original string value from your XML; they are actual date/timestamp with time zone types, my client is just using my NLS settings.)
You also have the option of using Oracle's built-in XML handling to extract relational data from your document, but that's rather off-topic and might not be appropriate.

Cast varchar type to date

I'd like to change a specific column in my PostgreSQL database from character_varying type to type date. Date is in the format yyyy:mm:dd
I tried to do:
alter table table_name
alter column date_time type date using (date_time::text::date);
But I received an error message:
date/time field value out of range: "2011:06:15"
When you cast text or varchar to date, the default date format of your installation is expected - depending on the datestyle setting of your session, typically set in your postgresql.conf. If in doubt, check with:
SHOW datestyle;
Generally, colon (:) is a time separator, In a simple cast, PostgreSQL will probably try to interpret '2011:06:15' as time - and fail.
To remove ambiguity use to_date() with a matching pattern for your dates:
ALTER TABLE table_name
ALTER COLUMN date_time type date
USING to_date(date_time, 'YYYY:MM:DD'); -- pattern for your example

Perl DBD::ODBC Issues with Oracle Date Formats

I am using Perl's DBD::ODBC to connect to an Oracle database. However, an issue arises when I try to execute a select query using a date in the where clause. It seems this issue occurs because of the database's date format being DD-MON-RR (see DBD::ODBC::FAQ). Since I cannot change the database's settings, can anyone suggest a workaround?
The database's default date format only matters if you depend on it, which you should not in general. You can:
1) Specify the format of the date in your query:
select *
from news
where news_date = to_date ('01-DEC-2009','DD-MON-RRRR');
2) Use the ANSI standard for date literals:
select *
from news
where news_date = DATE '2009-12-01';
One option is to use the TO_DATE() function (or the ANSI 'DATE' keyword) to convert the format in every query:
WHERE date_field > TO_DATE('2009-11-01', 'YYYY-MM-DD');
-- or
WHERE date_field > DATE '2009-11-01'
If you have to do this a lot, a better option would be to set the format for the session:
$dbh->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
Then:
my $sth = $dbh->prepare(<<EOT);
SELECT date_field
FROM some_table
WHERE date_field > '2009-11-01'
EOT
Don't rely on implicit datatype conversion. You can always specify the date format in the where clause:
WHERE your_column = to_date(:your_parameter, 'yyyy/mm/dd')