Format string to Datetime - sql

I am currently in the process of creating a query on our Oracle DB in Microsoft Query. Somehow, I only get the date from the Oracle DB as a string. The format looks like this: "YYYY-DD-MM SS:MM:HH".
Excel recognizes the column at the end only as a string.
How can I have the column output directly in the date format "DD.MM.YYYY"?
Can I change the format directly via Select?
SELECT
DB_Gen.STRT,
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (DB_Gen.STRT Between TRUNC(TO_DATE('01.01.2019','dd.mm.yyyy'))
AND TRUNC(TO_DATE('01.09.2019','dd.mm.yyyy')))
Do you even know how I can display the last 60 days?
So far, I'm only talking about Between and Trunc(TO_DATE .....
Best Regards
Joshua

How I can display the last 60 days?
where DB_Gen.STRT >= trunc(sysdate) - 60
How can I have the column output directly in the date format
"DD.MM.YYYY"?
select to_char(DB_Gen.STRT, 'DD.MM.YYYY') as strt

I think you're looking for
DB_Gen.STRT BETWEEN SYSDATE - 60 AND SYSDATE --Or CURRENT_DATE

What I prefer is to set NLS_DATE_FORMAT at session level to the desired format if I had to access dates in my query multiple times.
In your case, the following is my suggestion:
-- setting date format at session-level
alter session set NLS_DATE_FORMAT = 'DD.MM.YYYY';
SELECT
DB_Gen.STRT, -- this date will be populated in DD.MM.YYYY format in result
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (TRUNC(DB_Gen.STRT) BETWEEN TRUNC(SYSDATE-60) AND TRUNC(SYADATE);
-- Trunc is used to ignore time portion
Cheers!!

Related

Oracle's SQL how to convert the date's format

this is my table:
table name EXAMPLE
column name DATE
this is the output for the following query:
SELECT date
FROM example;
1/23/2010
I want to convert the output to:
23-Jan-10
Is it possible?
Thanks
You can use formatting along with TO_CHAR() conversion such as
SELECT TO_CHAR(dt,'DD-Mon-RR')
FROM example
where
DATE is not a good name for a column as being a reserved keyword. So, I've replaced it with dt
If your column is in varchar/text.. then you can first convert to date then back to char
SELECT
TO_CHAR(
to_date('1/23/2010','mm/dd/yyyy'),'DD-Mon-YY'
)
datec FROM dual;
If it is in date format, to_char only will do
SELECT
TO_CHAR(date_column, 'DD-Mon-YY' )
datec FROM dual;
A DATE data type is a binary format that is stored in 7-bytes that has no format and always contains the components: year (stored as century and year-of-century), month, day, hour, minute and second.
Therefore, you cannot change the format of a DATE data type.
If instead, you ask the question:
How can format a DATE to output it as 23-Jan-10?
Then you can convert the DATE to a formatted string using the TO_CHAR function:
SELECT TO_CHAR(your_date_column, 'DD-MON-YY', 'NLS_DATE_LANGUAGE=English')
AS formatted_date
FROM your_table;
Alternatively, if you want the output as a DATE data type then you can use:
SELECT your_date_column
FROM your_table;
and change the user interface (SQL/Plus, SQL Developer, Toad, PLSQLDeveloper, PHP, Java, etc.) you are using to alter how that that user interface displays dates. The solution is going to depend on which user interface you are using but for SQL/Plus and SQL Developer, you can change Oracle's NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YY';
Other user interfaces will have different solutions specific to those interfaces.

Is there any ways to implement in oracle sql column default, like today() in excel?

Is there any possible way to make the column in sql oracle be like today() function in excel. Which will be show the today's date. I've tried using "SYSDATE" in the default value column but it does not change day by day means it only take the submission date.
default value column I need it changes to the current date.
You need to select the column. The default value is assigned when a row is inserted.
One method would use a view:
create view v_t as
select t.*, trunc(sysdate) as today
from t;
If you want the value in a particular format, either set the format in Excel or use to_char(), such as to_char(sysdate, 'MM/DD/YYYY').
Try this
SELECT TO_CHAR
(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') "NOW"
FROM TableName;
I found that i can achieve that, if I : update tablename
set DATETODAY = TRUNC(SYSDATE) :
Is that efficient? Can we schedule the column to be update everyday/certain time

get four digit year from oracle date

I have a table A, which has a column fromdate.
select to_date(fromdate,'DD-Mon-YYYY') from A
returns value as 31-AUG-99
But I need to know whether the date is 31-AUG-1999 or 31-AUG-2099
Is there any one to help?
Use to_char function to get the date in character format.
Try this:
select to_char(fromdate,'DD-Mon-YYYY') from A;
Or if you want to want it in date then you have to change nls date settings.
alter session set nls_date_format = 'DD-Mon-YYYY'
before executing your original posted query.

Oracle 11g query

I am trying to write a query that will alter the required data in a table if another column meets certain criteria. It will need to compare the "Last_logon" column to the SYSDATE, if the Last_logon date was prior to the (SYSDATE - 45 days) I want it to change a column in the table called "STATUS" from 'Active' to 'Inactive'.
Below is what I have written so far but one issue is that the "Last_logon" column is data type varchar2 and is in the format Month, DD YYYY HH:MM. Any suggestions would be great I have tried to "convert" the varchar2 to a date data type but that only resulted in numerous error messages.
ALTER TABLE technician_tbl
ALTER COLUMN 'STATUS'
WHERE 'Last_logon' > (SYSDATE -45);
What about:
UPDATE technician_tbl
SET status = 'Inactive'
WHERE TO_DATE(last_logon, 'MONTH, DD YYYY HH:MM) < SYSDATE - 45;
COMMIT;
I assume that the column Last_logon is in fact called LAST_LOGON. If it really uses mixed case spelling, you would need to put its name in double quotes: "Last_Logon".
If have just tried it against a Oracle 9 database. What you are looking for is something similar to this:
Update Technician_tbl
Set Status = 'Inactive'
Where TO_DATE(Last_Logon, "MM.DD.YYYY HH:MM") > (SYSDATE -45)
Assuming your Last_Logon really has the format MM.DD.YYYY HH:MM. If last logon has another format, you need to change the To_Date() function accordingly.
If Last_Logon is in the format MONTH, DD YYYY HH:MM the function would be TO_DATE(Last_Logon, 'MONTH, DD YYYY HH:MM')
Since you are new to the world of SQL: If you are using SQL plus you might need to add a COMMIT to the end of your query in order to write your changes to your database.
Further information:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm
ALTER TABLE technician_tbl
ALTER COLUMN 'STATUS'
WHERE 'Last_logon' > (SYSDATE -45);
I don't believe that it is a valid query syntax in Oracle.
one issue is that the "Last_logon" column is data type varchar2 and is in the format Month, DD YYYY HH:MM.
You are struggling between a STRING and an actual DATE. But, it is not the main issue regarding your question.
if the Last_logon date was prior to the (SYSDATE - 45 days) I want it to change a column in the table called "STATUS" from 'Active' to 'Inactive'.
To manipulate data based on an action, requires a trigger or some alternative to suffice.

Filtering by date

I have a Date type column where are values in this format
1.1.2012 10:10:11
I need to create a filter which would filter these values by day, month and year.
I've tried
where like '% 1.1.2012 %'
but this seems to not working.
Oracle not store your date field formatted, but you can format the output with to_char function. For example:
select to_char(date_field,'dd/mm/yyyy hh24:mi:ss')
If you query a date without formatting, the output format will depend on the tool that you are using and your NLS_DATE parameter too.
To filter dates in Oracle you can use the to_date function, that receives an string and parse to date with some specific format. You can see all options of to_date here
Options to filter your date field:
where date_field between to_date('1.1.2012 00:00','d.m.yyyy hh24:mi') and to_date('1.1.2012 23:59','d.m.yyyy hh24:mi')
-- you possibly will lost some performance with this second one
where trunc(date_field) = to_date('1.1.2012','d.m.yyyy')
In MSSQL, you can use date-functions, that are easy to handle. One way would be like this:
where Year (date) = 2012
and Month(date) = 1
and Day (date) = 1
But there are other solutions. Take a look at the following page for mor information:
http://msdn.microsoft.com/en-us/library/ms186724.aspx
I worked recently with string-representations of datetime-values. I recommend not do it and to work always with the dates, because of compatibility, speaking of the MSSQL-Server.
If you use string-representations of datetime-values you need to be very careful with formats on different language-settings than the one on your own server.
Strings can be interpreted different on other servers (ISO-format vs us-format).
One possibility would be to do something like this:
WHERE date_and_time >=to_date( '01.01.2012','dd.mm.yyyy') and date_and_time <= to_date('01.01.2012','dd.mm.yyyy');
date_and_time is the name of your Date column.
edit: This is for Oracle