Splitting daterange into columns - sql

I have defined a table in PostgreSQL with a column of type daterange, and now need to split it into two columns of type date (start and end date). How can I do that, I was unable to find anything.
For example, I have [2012-01-01,2015-10-10) in column period and need: start_date 2012-01-01 and end_date 2015-10-10.

lower and upper
https://www.postgresql.org/docs/9.6/static/functions-range.html
create table t (period daterange);
insert into t (period) values ('[2012-01-01,2015-10-10)');
select (period).lower
,(period).upper
from t
;

Related

How to split a datetime column in to two columns of Date and time separately in Oracle SQL?

I have a datetime column.
I want two columns: a date and a time column.
How can I split my column into two?
Use:
a DATE data-type with the time component set to midnight for the date (you can enforce this with a check constraint); and
an INTERVAL DAY(0) TO SECOND data-type for the time component.
CREATE TABLE table_name(
datetime_column DATE,
date_column DATE,
time_column INTERVAL DAY(0) TO SECOND,
CONSTRAINT table_name__date_column__chk CHECK (date_column = TRUNC(date_column))
)
If you want to get the combined date-time then you can easily add the two to get back to a date-time value.
How can I split my column into two?
Assuming you have the columns you can use:
UPDATE table_name
SET date_column = TRUNC(datetime_column),
time_column = (datetime_column - TRUNC(datetime_column)) DAY TO SECOND;
db<>fiddle here
As Gordon commented, there's no time datatype in Oracle.
Though, literally answering what you asked, you can separate date and time and store each of them into their own columns - it's just that these will be VARCHAR2 columns and you can only look at how pretty they are. You can't, for example, do any date arithmetic on them; first you'd have to convert them back to date datatype, so question is what you really want to do with what you get.
Anyway, here you are:
SQL> create table test
2 (datum date,
3 date_only varchar2(10),
4 time_only varchar2(8)
5 );
Table created.
Sample value:
SQL> insert into test (datum) values (sysdate);
1 row created.
Split date to two parts:
SQL> update test set
2 date_only = to_char(datum, 'dd.mm.yyyy'),
3 time_only = to_char(datum, 'hh24:mi:ss');
1 row updated.
What's in there?
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * from test;
DATUM DATE_ONLY TIME_ONL
------------------- ---------- --------
05.08.2021 21:05:06 05.08.2021 21:05:06
SQL>
Since there is no specific datatype for time, here my suggestion would be to keep the datetime in main column and add two VIRTUAL COLUMN for date value and time value respectively.
Oracle 11g has introduced a new feature that allows you to create a VIRTUAL COLUMN, an empty column that contains a function upon other table columns (the function itself is stored in the data dictionary).
However, it all depends on what you are going to do with it.
Please elaborate your requirement so that you will get a more specific answer.

How to convert two types of dates format into one date format in SQL?

I have three table in the database two of them contain dates however the dates are in two format, first 20-02-2011 and second is 25/09/2018. Let say each table have 10000 records and mixed with these two types of dates format. This why I why I create the column like --- (Transaction_Date, Varchar(10) Not Null)
I tried convert (Varchar(10),Transaction_Date,105)
and also tried replace(convert(varchar(10),Transaction_Date,105),'/','-')
However date and year functions are still not working.
Please suggest a possible way.
How about this?
select replace(date, replace(Transaction_Date, '/', '-'), 105)
That is: (1) convert to a date and (2) replace the slash before converting.
You need to remember about your culture. Saved format vs server culture. But this is very possible
select Cast('2-22-2011' as datetime) f1,
Cast('2/22/2011' as datetime) f2
I other words just use Cast
select cast(Transaction_Date as datetime) . . .
But you should as soon as possible get rid of columns that saves date as string and create new date/time column, and insert your date values there
alter table tbl add column temp datetime
update tbl set temp = Cast(Transaction_Date as datetime)
alter table tbl drop column Transaction_Date
alter table tbl add column Transaction_Date datetime
update tbl set Transaction_Date = temp
alter table tbl drop column temp

SQL - How to format date in column

The dates provided in the Expiration Date column appear as just numbers, ie 09192019, I would like the entire column to be in a date format ie; 09/19/2019
Code:
SELECT Expiration_Date
FROM Insurance
datatype of the table column is probably not date. I suggest you to fix the datatype by setting it to date by altering your table.
for this, use the below steps:
alter table Insurance add column (expiration_date_new date);
update Insurance set expiration_date_new = TO_DATE(Expiration_Date, 'MM/DD/YYYY');
alter table Insurance drop column expiration_Date;
alter table Insurance rename column expiration_date_new to expiration_date;
as a workarround you can convert Expiration_date column to date by
SELECT TO_DATE(Expiration_Date, 'MM/DD/YYYY') FROM Insurance

Oracle 12c - how to set 'date' column with year quarter?

I have a need to create a column in Oracle 12c that will be date column with values:
20163
20164
20171
20172
20173
20174
...
How to specify that for a column in a create table statement?
thanks.
You can model the column as containing dates, constrained to fall only on the first moment of each quarter.
CREATE TABLE TEMP
( QUARTER DATE
CONSTRAINT IS_QUARTER CHECK ( (QUARTER = TRUNC(QUARTER, 'Q') ) )
)
To put values into the table, you need to set the date to the start of the quarter:
INSERT INTO temp VALUES ( TO_DATE('2017-04', 'yyyy-mm') );
To read values from the table, you can format as you like:
SELECT TO_CHAR( quarter, 'YYYYQ') FROM temp;
Because the underlying column is a date, you can do things like compare it with other dates, etc.
SELECT TO_CHAR( ADD_MONTHS( quarter, 3 ), 'YYYYQ') FROM temp;
Joe!
Such fields as you described are frequently using for monthly periods.
Typicaly they are encoded in integer datatypes.
If you were not on Oracle, Int32 would be perfect.
In Oracle the decision is not so clear.
I would prefer NUMBER(6,0) and simple constraint that field most be less than any possible date in future.
CHAR(6) - is fine too but constraints will be more complex.

How to add null values for all columns for missing dates and time in SQL

I have a table with user name, dates, time of day and value associated for every hour.
I want to add missing dates for each user for all times of the day with null valued for values.
when u created the set it null
create table tablename
(
colname as datetime null
)
this will set bydefault its value to null