Select date format ddmmyyyy from oracle database - sql

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.

Related

Date Time to String Snowflake

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.

How to convert Varchar to Date?

I have a table - Order and I have a field "Order_Date" which is in Varchar format - '20150804'.
select order_date from order
output- 20150804
I want to convert order_date- '20150804' into Date format- '07-SEP-15'
Try this:
select to_char(to_date('20150804', 'yyyymmdd'), 'dd-MON-yy') from dual;
In your actual query it would be like:
select to_char(to_date(order_date, 'yyyymmdd'), 'dd-MON-yy') from order

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

SQL query select data between two date fields not working

I have the following values in my table
Select * From TimeSheetLogs where InTimeStamp <= '1/22/2013'
when i execute the above query i get null value
but as you can see i have 3 data with datefield as 1/22/2013
Then what i am doing wrong?
Thanks
that is correct because
'1/22/2013 19:21' > '1/22/2013 00:00'
You probably need to truncate your dates first to remove the time portion. Then convert both sides to date datatype. The string '1/22/2013' is a string, not a date. By looking at data your InTimeStamp is timestamp datatype. You cannot compare date or timestamp to character w/out converting the char-s to date. I'm not sure what Database are you using. This is how you'd convert in Oracle using to_date function .
SELECT in_date, compare_date
FROM
(-- This is your InTimeStamp on the fly
SELECT trunc(to_timestamp('2013-01-22 16:21:19.273', 'yyyy-mm-dd hh24:mi:ss.ff')) in_date -- this is your InTimeStamp
, to_date('1/22/2013', 'mm/dd/yyyy') compare_date
FROM dual
)
WHERE in_date <= compare_date
/
Now you can compare two dates below - this is the output of above query:
in_date compare_date
---------- ------------
1/22/2013 1/22/2013
Select * From TimeSheetLogs
where InTimeStamp <= select convert(datetime,'1/22/2013',101)
As per my comment to #Zdravko's good answer, you just need to cast your date:
Select * From TimeSheetLogs where CAST(InTimeStamp as Date) <= '1/22/2013'