How to convert Varchar to Date? - sql

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

Related

How to select date from timestamp column in Oracle SQL Developer?

I have table in Oracle SQL Developer like below:
col1
--------
2019-11-29 11:14:00.821822
2020-02-11 09:14:00.821847
And I would like to select only rows where date is '2019-11-29' how can I do that?
You can use:
where trunc(col1) = date '2019-11-29'
However, that cannot use an index on col1. So, it is often better to use:
where col1 >= date '2019-11-29' and
col1 < date '2019-11-30'
You can to_date() or to_char() functions. The Oracle/PLSQL TO_DATE function converts a string to a date and TO_CHAR function converts a date to a String. The TRUNC(date) function returns date without the time portion.
select *
from myTable
where trunc(col1) = to_date('2019-11-29', 'yyyy-mm-dd');
or:
select *
from myTable
where to_char(col1, 'yyyy-mm-dd') = '2019-11-29'

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.

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.

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'

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"