Oracle: How to convert a string into date format - string

Have done a lot of search before asking this question, like How to convert a string into date format, How to convert a string into date, but still can't figure it out.
So, here's the question, how to convert these string dates into date in Oracle:
"2016-08-15 10:45:30" (String type) -> 20160815 (Date type)
"20160815104530" (String type) -> 20160815 (Date type)
Any idea will be appreciated.

You are mixing two things here:
The first is the conversion of a String data type, in Oracle VARCHAR2 into a DATE data type.
The DATE data type has a precision of seconds, you can't change that. A DATE data type will always give you the date and time component, i.e year, month, day, hours, minutes and seconds: Oracle SQL Data Type Documentation
However, the second part of what you are asking is about how to format the date when retrieved. This is helpful when running reports, or other kinds of visual display of dates. For example, in the US you would most likely want your date columns appear in the format MM/DD/YYYY while everywhere else in the world you most likely want to stick with DD/MM/YYYY. Oracle lets you do that by telling it what NLS_DATE_FORMAT you want to use. You can set that parameter for each individual session as well as on database level, it is up to you (and your DBA) to decide where and when you want to set that. In your case you can apply this via the ALTER SESSION command:
SQL> ALTER SESSION SET nls_date_format='YYYY-MM-DD';
Session altered.
SQL> SELECT TO_DATE('2016-08-15 10:45:30', 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
TO_DATE(
----------
2016-08-15
SQL> SELECT TO_DATE('20160815104530', 'YYYYMMDDHH24MISS') FROM DUAL;
TO_DATE(
----------
2016-08-15

You use to_date():
select to_date(substr(str1, 1, 10), 'YYYY-MM-DD')
select to_date(substr(str2, 1, 8), 'YYYYMMDD')

Related

varchar yy/mm/dd to date azure sql server

Im trying to convert a varchar value into a date format.
The varchar value is 99/01/30and the result should be 1999-01-30.
Would somebody help me with this?
I'm trying to do this but :
select convert(date, convert(varchar, '99/01/30', 11),23) as date
but this gives me an error: Conversion failed when converting date and/or time from character string.
This
select convert(date, '99/01/30', 2) as date
will get you what you want. See also this page for more information about what the 2 means

Hive TimeStamp column with TimeZone

I have a created a table in hive with one column as timestamp datatype. While I am inserting into the hive getting different than the existing.
My column expected value : 2021-11-03 16:57:10.842 UTC (This I am getting as string). How I can store the above output in hive table( column with Datatype as timestamp)
You need to use cast to convert this to timestamp after removing the word UTC. Since hive doesnt care about timezone intentionally, and display data in UTC, you should be in good shape.
select cast( substr('2021-11-03 16:57:10.84 UTC',1,23) as timestamp) as ts
Pls note you need to have the data in above yyyy-MM-dd hh:mm:ss.SS format.
Also pls note you can not use from_unixtime(unix_timestamp(string_col , 'dd-MM-yyyy HH:mm:ss.SSS')) because we will loose millisecond part.

Specify datetime2 format in Azure SQL data warehouse (synapse)

What is the correct way to specify the format of a datetime2 field when creating a table in Azure SQL data warehouse? I don't seem to be able to find an example in the documentation.
The data looks like this:
"2020-09-14T20:50:48.000Z"
CREATE TABLE [Foo].[Bar](
...
MyDateTime datetime2(['YYYY-MM-DDThh:mm:ss[.fractional seconds]')
)
As Panagiotis notes, the underlying representation is an int/long for the actual date value. This is how RDBMS engines can quickly compute the delta between two dates (days between Monday and Friday is a simple subtraction problem). To answer your question, you simply would format your create table as:
CREATE TABLE [Foo].[Bar](
...
MyDateTime datetime2
)
If you're interested in formatting the result in a query, you can look to the CONVERT or FORMAT functions. For example, if you wanted the format dd-mm-yyyy (Italian date), you could use either of the following:
SELECT
CONVERT(VARCHAR, CURRENT_TIMESTAMP, 105)
, FORMAT(CURRENT_TIMESTAMP, 'dd-MM-yyyy')
Note: CONVERT is generally faster than FORMAT and is the recommended approach if you have a date format that is supported. This is because the FORMAT function relies on the CLR which will include a context/process jump.

re-format timestamp to keep time and time zone

I want to create a table with date and timezone in different columns.
For example:
Date 20170311 Time 10:32:24+1300
The format has to be the same as above.
When I create the table Date was set as type date and time was type timestamp.
When I insert the date, I have to follow a certain format like 2017-03-11, how can I make it the same as the table shown.
When inserting the time and time zone, I have to insert the date alone with it, like '2017-03-22T10:37:50+1300' is there any way that I can reformat it?
After inserting with this format '2017-03-22T10:37:50+1300', the time and time zone changed in the table, how could I keep it the same as input?
CREATE TABLE example (id int, work_date date, sequence timestamp);
INSERT INTO example (id int, work_date date, sequence timestamp) VALUES (1, '2017-03-22', '2017-03-22T10:37:50+1300')
expected result:
1 20170322 10:37:50+1300
actual result:
1 2017-03-22 2017-03-21 21:37:50.000000+0000
Cassandra has several data types related to date & time - date, time, and timestamp, and only the last one has the notion of the time zone.
The formatting of the timestamps is your responsibility - internally data is stored as long (8 bytes) representing number of milliseconds since epoch, and then converted into textual representation by corresponding driver - in case of cqlsh, the formatting is controlled by datetimeformat parameter. Similarly, for date & time data types - they are kept as numbers inside database, not as strings.
If you're accessing the data from your own program, then you can format time as you want.

How do I query for a specific day such as yesterday in Core Data?

In plain SQL (in my case: sqlite), I would be able to query for a specific date in a DATE column as follows:
SELECT * FROM table WHERE date(dateColumn) = '2015-01-01'
This works because the date() function cuts off the time part of the DATE value.
Can I do something similar with Predicates in Core Data? Or do I have to use something where I determine the start and end of that day and then look for dates between the two date values?

Resources