Postgres Timestamp to DATE dd-mm-yyyy - sql

im trying to insert select a casted timestamp in a date colum timestamp like 02-09-2021 00:00:00, and i need to convert this timestamp to date dd-mm-yyyy without hhmmss, i've tried select date(column) as newdate but when i check my table insert, it keeps like timestamp, all the solutions that i tried only runs perfectly only in a select sentence, but when i try to insert select.. i keep with timestamp type..

If you need to convert formatted text to timestamp then use to_timestamp with explicit format specification. Try this:
select to_timestamp('02-09-2021 00:00:00', 'dd-mm-yyyy hh24:mi:ss');
to_timestamp
2021-02-09 00:00:00.000 +0200
To convert it to date:
select to_date('02-09-2021 00:00:00', 'dd-mm-yyyy');
If you need to change the type of an existing timestamp column to date then:
alter table the_table alter column the_column type date using the_column::date;

Related

Get date and time from date column in oracle

I inserted date and time in database, now when trying to retrieve date and time both from database only getting time part.
I tried insert date using TO_DATE('08/13/2019 09:10:03', 'MM/DD/YYYY HH24:MI:SS') into 'Time' col. Now trying to get date from table using TO_DATE(Time, 'DD/MON/RR HH24:MI:SS'), but only getting date part. My database nls date format is "DD/MON/RR". 'Time' col is date type and I'm using oracle 10g xe.
I can get date and time using TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS') but as I need to use this in comparison operation like below:
select TO_CHAR(Time,'DD/MON/RR HH24:MI:SS') from Table where (TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS') <= TO_DATE('08/07/2019 10:13:52', 'MM/DD/YYYY HH24:MI:SS'))
it gives this error 'ORA-01830: date format picture ends before converting entire input string'. Also tried to use TO_DATE(TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS')), still it gives only time part. Should I use TIMESTAMP datatype for 'Time' col?
I want to get date and time from table where i can use them in comparison operation.
Date need not be converted into date again.
You can simply write your query like this:
SELECT
TIME -- use to_char for formatting the output date
FROM Table
WHERE
TIME <= TO_DATE('08/07/2019 10:13:52', 'MM/DD/YYYY HH24:MI:SS')
Cheers!!

Apache Hive: converting timestamp from string to timestamp and save it table

I have a table like this in hbase:
tableExaple (timestamp, timestamp_string, someOtherStuff)
timestamp has the datatype timestamp
timestamp_string hast the datatype string and has the pattern 'yyyy-MM-dd HH:mm:ss.SSS'
Now I would like to read the value from timestamp_string convert it with the hive-UDF unix_timestamp(string date, string pattern) to a timestamp and save this in the same table to the value timestamp.
How can I do this?
Stuff like
INSERT INTO tableExaple (timestamp) SELECT unix_timestamp(timestamp_string, 'yyyy-MM-dd HH:mm:ss.SSS') FROM tableExaple;
does not work.
I don't think unix_timestamp likes the millisecond format. Since timestamp_string is of type string, you can just split on the '.' and grab the date and time. So if you had 2014-11-11 08:09:10.123, split(timestamp_string, '\\.') will give you [2014-11-11 08:09:10, 123]. You can now reference your array by [0] and [1].
Example:
SELECT otherStuff
, timestamp
, unix_timestamp(split(timestamp_string, '\\.')[0], 'yyyy-MM-dd HH:mm:ss') new_time
FROM some_table
If you want to include the milliseconds in your new_time column, just grab the [1]th index from the split array (and divide by 1000).

How to convert timestamps' timezones in order to compare them in Oracle DB?

We are using Oracle 11g and we have a table with a timestamp column which doesn't contain the timezone (simply defined as TIMESTAMP). The timezone is written in a different column.
I want to be able to use the 'max' function while taking the timezone into account.
There's probably a function to convert timezones easily and I just don't know it.
I want to be able to do something like this:
SELECT max(covert_to_timezone(my_timestamp, my_timezone, 'GMT')) FROM my_table
How can I do that?
==================
Solution: According to Ajith Sasidharan's answer:
SELECT max(from_tz(my_timestamp, my_timezone) at time zone '0:00') FROM my_table
Update: If you want to compare dates, simply use 'cast':
SELECT max(from_tz(cast(my_date as timestamp), my_timezone) at time zone '0:00') FROM my_table
Btw, now I got 'ORA 01878' errors, meaning our table contains invalid times (DST shit).
I guess you want a function like this ::
select to_char((from_tz(to_timestamp(to_char(DATABASE_DATE, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/New_York')
at time zone 'America/Los_Angeles'),'YYYY-MM-DD HH:MI:SS PM TZD') as localtime
from table

How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL?

I have a date column in a table stored as MM/DD/YYYY format. I have to select and store the same date in another table in YYYY-MM-DD format i.e. XSD Date Format. But I am not able to do it. I am using this query:
select to_date(date_column,'YYYY-MM-DD') from table;
But still I am not able to do it. Giving me error
ORA-01843 : not a valid month
use
select to_char(date_column,'YYYY-MM-DD') from table;
It sounds like you've got it the wrong way round. If your existing data is in MM/DD/YYYY format, then you want:
select to_date(date_column,'MM/DD/YYYY') from table;
to convert the existing data to DATE values. (I do wonder why they're not stored as dates, to be honest...)
If you want to perform the conversion in one step, you might want:
select to_char(to_date(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD') from table;
In other words, for each row, parse it in MM/DD/YYYY format, then reformat it to YYYY-MM-DD format.
(I'd still suggest trying to keep data in its "natural" type though, rather than storing it as text in the first place.)
I assume that you can use the Oracle SQL Developer, which you can download from here.
You can define the date format which you want to work with:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
With this, now you can perform a query like this:
SELECT * FROM emp_company WHERE JDate = '2014-02-25'
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
To convert a DATE column to another format, just use TO_CHAR() with the desired format, then convert it back to a DATE type:
SELECT TO_DATE(TO_CHAR(date_column, 'DD-MM-YYYY'), 'DD-MM-YYYY') from my_table
select to_date(to_char(ORDER_DATE,'YYYY/MM/DD'))
from ORDERS;
This might help but, at the end you will get a string not the date. Apparently,
your format problem will get solved for sure .
For military time formatting,
select TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mm:ss') from DUAL
--2018-07-10 15:07:15
If you want your date to round DOWN to Month, Day, Hour, Minute, you can try
SELECT TO_CHAR( SYSDATE, 'yyyy-mm-dd hh24:mi:ss') "full-date" --2018-07-11 10:40:26
, TO_CHAR( TRUNC(SYSDATE, 'year'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-year"-- 2018-01-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'month'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-month" -- 2018-07-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'day'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-Sunday" -- 2018-07-08 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'dd'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-day" -- 2018-07-11 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'hh'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-hour" -- 2018-07-11 10:00:00
, TO_CHAR( TRUNC(SYSDATE, 'mi'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-minute" -- 2018-07-11 10:40:00
from DUAL
For formats literals, you can find help in
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions242.htm#SQLRF52037
You can do this simply by :
select to_char(to_date(date_column, 'MM/DD/YYYY'), 'YYYY-MM-DD') from table
According to the comments, the data-type in the datatable is DATE.
So you should simply use:
"select date_column from table;"
Now if you execute the select you will get back a date data-type, which should be what you need for the .xsd.
Culture-dependent formating of the date should be done in the GUI (most languages have convenient ways to do so), not in the select-statement.
Basically , Data in a Date column in Oracle can be stored in any user defined format or kept as default.
It all depends on NLS parameter.
Current format can be seen by : SELECT SYSDATE FROM DUAL;
If you try to insert a record and insert statement is NOT in THIS format then it will give :
ORA-01843 : not a valid month error.
So first change the database date format before insert statements ( I am assuming you have bulk load of insert statements) and then execute insert script.
Format can be changed by :
ALTER SESSION SET nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
Also You can Change NLS settings from SQL Developer GUI , (Tools > preference> database > NLS)
Ref: http://oracle.ittoolbox.com/groups/technical-functional/oracle-sql-l/how-to-view-current-date-format-1992815
This worked for me! You can convert to datatype you want be it a date or string
to_char(TO_DATE(TO_CHAR(end_date),'MM-DD-YYYY'),'YYYY-MM-DD') AS end_date
Late reply but for.databse-date-type the following line works.
SELECT to_date(t.given_date,'DD/MM/RRRR') response_date FROM Table T
given_date's column type is Date
Just to piggy back off of Yahia, if you have a timestamp you can use this function to cast exclusively as date, removing the timestamps.
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYY-MM-DD') AS TrackerKey__C
Or in my case I need the below format
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYYMMDD') AS TrackerKey__C
SELECT TO_DATE(TO_CHAR(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD')
FROM table;
if you need to change your column output date format just use to_char this well get you a string, not a date.
use
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
also gothrough
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

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;