Convert varchar2 to date function in View - sql

I have a column in view thats has literals as '26Jul2022 10:05:52:209' and has datatype as varchar2. I need to convert this column datatype into Date. How can I do that?

Oracle's date datatype doesn't store milliseconds, but you could use the timestamp datatype instead by calling to_timestamp:
SELECT *, TO_TIMESTAMP(some_column, 'DDMONYYYY HH24:MI:SS:FF3')
FROM some_table

Related

can we insert date into varchar2 in oracle without converting by to_char?

can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.

How to convert timestamp to date&time in SQL?

Table have one column which contain timestamp value.I want to convert that timestamp value to date&time in SQL.
When I using like this
select cast(1520339311 as datetime)
I'm getting an error:
Arithmetic overflow error converting expression to data type datetime
How to handle that?
SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
See the links Below:
CAST and CONVERT (Transact-SQL)
rowversion (Transact-SQL)
Already Answered

oracle db column date default value not sysdate

how to set the default value for a date column for something diferent that sysdate? like 01/07/1998, im currently using
ALTER TABLE XILIADO
ADD (FECHA_AFIL DATE DEFAULT sysdate NOT NULL);
Just use a fixed date, either with to_date() with a string literal and matching format model, or a date literal:
ALTER TABLE XILIADO
ADD (FECHA_AFIL DATE DEFAULT DATE '1998-07-01' NOT NULL);
(assuming your example was in DD/MM/YYYY format; date literals are unambiguous, which is another advantage of using them.)
For any fixed date, use to_date(date_value, date_mask) along with alter column.
ALTER TABLE XILIADO
ADD FECHA_AFIL DATE DEFAULT TO_DATE('01-07-1998','DD-MM-YYYY') NOT NULL

Insert Timestamp into Varchar2 column in Oracle Database

I am working with an application that interfaces with an Oracle 11g database. I need to insert a timestamp in the YYYYMMDDhhmmss format, into a Varchar2 column.
My SQL update query is:
update CODELIST_XREF_ITEM set RECEIVER_ITEM=TIMESTAMP where LIST_NAME='BUSINESS_PROCESS';
The TIMESTAMP variable is where it need to be inserted. I have tried setting the TIMESTAMP variable to CURRENT_TIMESTAMP, however I am not able to format that.
Any suggestions?
Thanks in advance for all your help!
If you want to insert the character representation of a timestamp (it actually appears that you want the character representation of an Oracle date since you don't want fractional seconds or a time zone), you'd use the to_char function.
to_char( sysdate, 'YYYYMMDDHH24MISS' )
So your UPDATE statement would be
update CODELIST_XREF_ITEM
set RECEIVER_ITEM=to_char( sysdate, 'YYYYMMDDHH24MISS' )
where LIST_NAME='BUSINESS_PROCESS';
If you wanted to store fractional seconds or a time zone or do something else that required a timestamp, you'd use the same to_char function just with a different format mask.

SQL oracle beginner questions

Question1:
Do i have to use to_date while inserting date?
INSERT INTO some_table (date1, date2)
VALUES (to_date('2012-10-24','YYYY-MM-DD'), to_date('2012-10-24','YYYY-MM-DD'));
Or can just insert as string? Will everything be OK this way too? I've tried and it worked.
INSERT INTO some_table (date1, date2)
VALUES ('2012-10-24',2012-10-24');
Question2:
What happens if i won't name columns that i'm inserting into? It works, but my question is if it inserts randomly now or it takes order of columns during creation of table?
INSERT INTO some_table
VALUES ('2012-10-24',2012-10-24');
1 seems to only work with the 'YYYY-MM-DD' format:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements003.htm#SQLRF51049 says
You can specify a DATE value as a string literal ... to specify a DATE value as a literal, you must use the Gregorian calendar. You can specify an ANSI literal... The ANSI date literal contains no time portion, and must be specified in the format 'YYYY-MM-DD'.
However, it might work with time if you use the
Alternatively you can specify an Oracle date value... The default date format for an Oracle DATE value is specified by the initialization parameter NLS_DATE_FORMAT.
For question 2, it uses the order at definition of the table. However you have to give values for all columns in that case.
Oracle supports Standard SQL date literals (since 9i).
It's DATE followed by a string with 'yyyy-mm-dd' format
DATE '2014-05-10'
It's much shorter than TO_DATE and it's independent of any NLS settings.
Similar for timestamps:
TIMESTAMP '2014-05-10 09:52:35'
Regarding your 2nd question: It's the order of columns as defined within the CREATE TABLE.
You could even do it like this one:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM:YYYY:DD';
INSERT INTO some_table (date1) VALUES ('05:2014:10');
...but doing it like this is not recommended. Use TO_DATE or DATE Literal, e.g. DATE '2014-05-10' instead. It makes your life easier.