Date Time to String Snowflake - sql

Need to convert a Snowflake TIMESTAMP_TZ(9) to String format,but throwing this error
"Date '27/02/2020' is not recognized"
Tried all of these:
TO_CHAR( date, 'DD-MM-YYYY') as date,
TO_VARCHAR(date, 'DD/MM/YYYY') as date,
TO_CHAR( date, 'DD.MM.YYYY') as date,

I can reproduce the problem if I write a query like this:
select to_char(date, 'DD-MM-YYYY')
from (
select '27/02/2020'::date date
)
-- Date '27/02/2020' is not recognized
And I can fix that query with this:
select to_char(date, 'DD-MM-YYYY')
from (
select to_date('27/02/2020', 'DD/MM/YYYY') date
)
So if that's your problem, first you need to parse the dates with to_date() in that particular format.

Related

DATE Manipulation

I would appreciate any pointers with this, I'm trying to ultimately get the Day of the week for each date. Unfortunately my DATESTRG in format 02-JUL-13 is ending up as 13/07/0002 rather than 02-07-2013 ( European date format ), and I get an error when I try to get the Day of Week DOW. Thank you.
WITH DATEDATE AS
(
SELECT
SUBSTR ( SRT.CREATED_DATE,1,10) AS DATESTRG
FROM SMS.REVIEW_TEXT SRT
)
SELECT
DATESTRG,
TO_DATE ( DATESTRG, 'YYYY-MM-DD' )
TO_CHAR ( DATE DATESTRG, 'DY') AS DOW
FROM DATEDATE
Try this:
WITH DATEDATE AS
(
SELECT
SUBSTR ( '2017-09-08',1,10) AS DATESTRG
FROM dual
)
SELECT
TO_CHAR (to_date(DATESTRG,'YYYY-MM-DD'), 'DY') AS DOW
FROM DATEDATE

Oracle query time on date fields

I have a column of type DATE stored data are contains date and time . I can see value when i do
select CAST(MSG_DT AS TIMESTAMP) from table;
this is the output
17-MAR-08 15:38:59,000000000
I have to select the row using
Only date
select CAST(MSG_DT AS TIMESTAMP) from
MWRB_RECEIVE where
MSG_DT >= TO_DATE( '2000-02-03' ,'YYYY-MM-DD')
and
MSG_DT <= TO_DATE( '2010-02-03' ,'YYYY-MM-DD')
Only time (eg: every message between 12:00:11 and 23:02:55)
In DB2 i can do
SELECT *
FROM TABLE
WHERE DATE(INS_TMS) = '2014-02-18'
SELECT *
FROM TABLE
WHERE TIME(INS_TMS) > '09.55.00'
In ORACLE I can't see the equivalent.
Try this:
SELECT *
FROM your_table
WHERE TO_CHAR (start_date, 'yyyy-mm-dd') = '2014-10-06'
AND TO_CHAR (start_date, 'hh24:mi:ss') > '10:00:00'
Why are you casting the column value to a TIMESTAMP when the column in the database is a DATE type? The fractional part of the seconds will always be 0, as DATE only has resolution to the seconds value. You need to add the hours,minutes, and seconds format specifier to the query:
select MSG_DT from
MWRB_RECEIVE
where MSG_DT between TO_DATE( '2000-02-03 12:00:11' ,'YYYY-MM-DD HH24:MI:SS') AND
TO_DATE( '2010-02-03 23:02:55' ,'YYYY-MM-DD HH24:MI:SS')
There is no need to split date and hour, you can have it in a single where clause
where field > to_date('20121212 12:12:12, 'YYYYMMDD HH24:MI:SS')
Check for your reference oracle to_date() as it seems the only thing you need

Insert date in oracle

When i executed the below query in Oracle
select TO_CHAR((CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS') from dual;
O/P : 04-Mar-2014 14:25:14
I would like to select current date only without current time as below
select TO_CHAR(trunc(CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS') from dual;
O/P : 04-Mar-2014 00:00:00
To achieve the only way is to apply function trunc() on the query? Is there any another way?
Edit : Thanks for your ans.Can it be done without any function?(wihout using to_char or trunc)
{sorry for missing this info}
The answer is simply no, there is no function that only gets the date part of the date / time (even current_date or sysdate are functions after all).
You should always use trunc to get the current date, without the time.
It isn't necessary to do a trunc and a to_char together. Keep to_char and don't specify the time part.
This is sufficient:
To get the date as varchar:
select TO_CHAR(CURRENT_DATE,'DD-Mon-YYYY') from dual
To get the date as date, with the time part as 00:00:00:
select trunc(CURRENT_DATE) from dual
You can do this:
select TO_CHAR(CURRENT_DATE,'DD-Mon-YYYY')||' 00:00:00' from dual;
there is also EXTRACT function which can be used like that:
SELECT extract(DAY FROM sysdate)
||'-' ||
extract(MONTH FROM sysdate)
|| '-' || extract(YEAR FROM sysdate)
FROM dual;
result: 4-3-2014
Use This Query...
select (TO_CHAR(TRUNC(CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS')) from dual

Select date format ddmmyyyy from oracle database

I want to select a date field from oracle database, using
select to_date(trndt, 'yyyymmdd') from cf20gldt, but the system was showing an error.
Reference
Change it as to_char
SELECT TO_CHAR(trndt, 'yyyymmdd') FROM cf20gldt;
if your trndt column is not date type then you have to change it as
SELECT TO_CHAR(TO_DATE(trndt), 'yyyymmdd') FROM cf20gldt;
For example display system date
SELECT TO_CHAR(sysdate,'yyyymmdd') FROM dual;
To_date is to convert a column to date format . If your trndt is not date already then use below :
select to_char(to_date(trndt), 'yyyymmdd') from cf20gldt;
Or if your column trndate is already in date format then use below:-
select to_char(trndt, 'yyyymmdd') from cf20gldt;
You could use:
SELECT to_char(sysdate, 'yyyy-mm-dd') date,
to_char(sysdate, 'hh24:mm:ss') hr
FROM DUAL;
Change sysdate by your field type Date.

Is it possible to order by a value which I dont want select in oracle database

I want to order values from database query by date but I want also to change their format
SELECT to_char( date, 'DD.MM.YYYY') "date1", date as date2
from ..
order by date2
This SQL has just one problem. I don't want date2 int the output.
Thanks for your help
Just use
order by date
or
order by tableName.date
use this:
SELECT to_char( date, 'DD.MM.YYYY') "date1"
from ..
order by date
try this:
SELECT to_char( date, 'DD.MM.YYYY') "date1"
from ..
order by to_date(date,'DD-MM-YYYY')
I suspect the issue you are running into is that the column is named date, which is an Oracle reserved word. If that is the case, you need to enclose it in quotes in order to avoid the error.
SELECT to_char( "DATE", 'DD.MM.YYYY') "date1"
from tablename
order by "DATE"