Day of the week from Timestamp , SQL - sql

I have a timestamp column in my table, out of which i want to create a new column - day of week - (Ex. Monday, Tuesday ) to the table. I'm using PGAdmin4

This will get the day of the week, replace GETDATE() with your timestamp.
SQL Server
SELECT DATENAME(dw,GETDATE())
PostgreSQL
select to_char(now(), 'Day')

Related

Inserting Day of week and Time in Oracle date column

Considering the following SQL instruction what should I change in order to insert only the day of the week and the time like Hour and Minutes:
INSERT INTO SALA_MATERIA(SALA_ID, MATERIA_ID,HORARIO) VALUES (1,'PT', TO_DATE('FRIDAY 15:00','DAY HH24:MI'));
A DATE always has year, month, day, hours, minutes and seconds. Trying to create a date from just a day of the week and a time does not make sense.
If you want to store just that information then use a VARCHAR2 column and not a DATE.
If you want to store it as a DATE then you will need to specify which Friday and specify a year, month and day as well.
I think you're actually better off storing an entire date - year, month, day, and time - then you can simply use TO_CHAR() or EXTRACT() to get the parts of the date you need. To store a Friday in a DATE column you could do the following:
INSERT INTO sala_materia
( sala_id, materia_id, horario )
VALUES
( 1, 'PT', NEXT_DAY(SYSDATE, 'FRIDAY') );
This will get the current time of day as well; to use an arbitrary time you could truncate the results of the NEXT_DAY function and then use intervals or standard Oracle date arithmetic:
INSERT INTO sala_materia
( sala_id, materia_id, horario )
VALUES
( 1, 'PT', TRUNC( NEXT_DAY(SYSDATE, 'FRIDAY') ) + INTERVAL '15' HOUR );
or
INSERT INTO sala_materia
( sala_id, materia_id, horario )
VALUES
( 1, 'PT', TRUNC( NEXT_DAY(SYSDATE, 'FRIDAY') ) + 15/24 );
Intervals might work a bit better if you need to be more granular (i.e., minutes and seconds).
The reason I recommend this course of action instead of merely storing the day of the week and the time in a VARCHAR2 column is that you can then use TO_CHAR() or EXTRACT() when displaying the date, using date parts in comparisons, etc. - you could even create function-based indexes or virtual columns based on the results of these functions applied to the horario column:
CREATE INDEX mydayofweek ON sala_materia ( TO_CHAR(horario, 'DAY') );

Filter timestamp by specific month-year

I am new to postgres and would appreciate any advice. I have postgres table with a timestamp column whose values are in the format: 1970-01-01 00:00:00
My objective is to select records from the last three whole months - December 2016, January 2017 and February 2017. How would one write this query with only read access using SELECT?
When I start with:
SELECT to_char("start_time", 'YYYY-MM-DD HH:MM:SS') FROM trips;
Times are converted to AM/PM but I am only interested in extracting and subsetting by month and year
Here you go:
SELECT *
FROM trips
WHERE start_time BETWEEN '2016-01-01 00:00:00'::timestamp AND '2017-02-28 23:59:59'::timestamp;
You can use extract or date_trunc function to extract month in postgresql.
Very similar to question get last three month records from table
For more details about date time functions in postgresql use below link.
https://www.postgresql.org/docs/9.1/static/functions-datetime.html
Here is one method:
select t.*
from t
where start_date >= date_trunc('month',now() - interval '3' month) and
start_date < date_trunc('month', now());

DB2 to Oracle Conversion For Basic Date Time Column Between Clause

What this is doing is selecting all columns from TABLE where a specific date time column is between last Sunday and this coming Saturday, 7 days total (no matter what day of the week you are running the query on)
I would like to have help converting the below statement into Oracle since I found out that it will not work on Oracle.
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN
current date - ((dayofweek(current date))-1) DAYS
AND
current date + (7-(dayofweek(current date))) DAYS
After poking around a bit more I was able to find something that worked for my specific problem with no administrator restrictions for whatever reason:
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN
TIMESTAMPADD(SQL_TSI_DAY, DayOfWeek(Current_Date)*(-1) + 1, Current_Date)
AND
TIMESTAMPADD(SQL_TSI_DAY, 7 - DayOfWeek(Current_Date), Current_Date)
Use TRUNC() to truncate to the start of the week:
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN trunc(sysdate, 'WW')
and
trunc(sysdate + 7, 'WW');
sysdate is the current system date, trunc truncates a data, and WW tells it to truncate to the week (rather than day, year, etc.).
Assuming DATE_TIME_COLUMN is - as it should be - of datatype date, I think this gets what you want.
where DATE_TIME_COLUMN between
next_day(sysdate,'SUNDAY')-7 and next_day(sysdate,'SATURDAY')
You may need to tweak it a bit. Please follow up studying the official docs on the NEXT_DAY function in the relevant docs at http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions106.htm#SQLRF00672
The proposed TRUNC does not guarantee you get the date of a particular day of the week:
SQL> alter session set nls_date_format='day dd-mon-yyyy';
Session altered.
SQL> select trunc(sysdate,'WW') from dual;
TRUNC(SYSDATE,'WW')
---------------------
friday 22-jan-2016
SQL> select trunc(sysdate+7,'WW') from dual;
TRUNC(SYSDATE+7,'WW')
---------------------
friday 29-jan-2016

Get name of weekday in netezza

I can get week number using extract dow function in netezza for a date.
Select extract(Dow from date) from table
How can I get name of the weekday?
I haven't tried with a date datatype, but to get the day name from a timestamp you would use
select to_char(date, 'Day') from table
That should give results of Sunday, Monday, Tuesday, etc. Try it with your date column and please let us know if it works.

In oracle SQL, how would you obtain the timestamp representing the start of the week?

I'm using an Oracle 9i database and want to obtain, within a function, the timestamp representing the start of the week, i.e. The most recent monday, at 00:00:00.
I am aware that the timestamp representing the start of the current day is TO_TIMESTAMP(SYSDATE).
You can use the function next_day to get that:
SQL> select next_day(sysdate-7, 'MONDAY') FROM DUAL;
NEXT_DAY
---------
29-APR-13
Getting the start of the week should work with trunc (see docs).
So,
select to_timestamp(trunc(sysdate, 'D')) from dual
should work.
However, depending on your NLS settings, the first day of the week for oracle may well be Sunday.
this appears to return Monday before the day of week in question at midnight. to prove out just play around with sysdate and subtract days...
select case when to_Char(sysdate,'d') = 1 then
trunc(sysdate-6)
else
trunc(sysdate - (to_Char(sysdate,'d')-2))
END
from dual;
You can truncate a date to Monday with:
select trunc(sysdate, 'IW') FROM DUAL;