How to create 'Week column' based on another date column? - date

I have column 'jobstarttimeiso' and I want to create another column for Weeks of the year based on the date. How would I go about doing that? I am using Redash to query from redshift database. Please help! Thank you.

The extract function will return a week number e.g.
select extract(week from jobstarttimeiso) as weeknumber
In general:
EXTRACT ( datepart FROM { TIMESTAMP 'literal' | timestamp } )
See: https://docs.aws.amazon.com/redshift/latest/dg/r_EXTRACT_function.html
alternatively you can also use to_char()
TO_CHAR (timestamp_expression | numeric_expression , 'format')
with the parameter IYYY as the format for ISO 8601 week-numbering year (4 or more digits)

Related

How to extract Year and Month in SQL Postgres by using Data_Part function

I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.

SAS Date conversion from text YYYY/MM to DATETIME20. Format

Hi I have a date format in one table in Text 'YYYY/MM'. example 2018/01, 2020/08 etc.
I need to join it with another table where the date is in Number type( and DATETIME20 format attached it it) and convert it to month and compare.
Is there any way to do it in PROC SQL as the rest of my query is in PROC SQL?
eg. Table 1: Month= 2018/01; Table 2: Date =20.01.2018 10:48:17 . They should be joined in the PROC SQL query.
I would also like to calculate difference in Months between these two dates.
Thank you in advance.
Convert both to the same DATE value. To convert a datetime value to a date use the DATEPART() function. To move to the first day of the month use the INTNX() function with the month interval. To convert a string like '2018/01' to a date you could use INPUT() function with YYMMDD informat by appending '/01'.
proc sql ;
create table want as
select *
from table1,table2
where input(cats(table1.month,'/01'),yymmdd10.)=intnx('month',datepart(table2.date),0)
;
quit;

Select date only without the hour on postgresql [duplicate]

This question already has answers here:
Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL
(8 answers)
Closed 6 years ago.
I have date column on my postgres table as timestamp format i.e "2017-01-01 22:00:00". When I wrote in the queries
select date from table where date = '2017-01-01' it did not give me any result.
Should I always include the time information on those queries? Can I just put the yyyy-mm-dd only on my queries to search the date column?
regards
Cast to date:
select "date" from table where "date"::date = '2017-01-01'
Note that I enclosed references to the date column in double quotes, because date is a Postgres keyword. You should avoid naming your columns, tables, or schemas using keywords.
Use date() function, it will extract date from datetime like:
select DATE(my_field) from my_table;
Try this way:
select date("Date") from yourTable

how to convert 12 hours timestamp format to 24 hours timestamp format in postgres?

how to convert 12 hours timestamp format to 24 hours timestamp format in postgres? like '2016-07-01 01:12:22 PM' to '2016-07-01 13:12:22'
Using PostgreSQL:
To convert 24 hours to 12 hours:
select to_char( to_timestamp ( '13:00:00', 'HH24:MI:SS' ) , 'HH12:MI:SS PM' )
from emp_table;
To convert 12 hours to 24 hours:
select to_char( to_timestamp ( '11:00:00' , 'HH12:MI:SS' ) , 'HH24:MI:SS AM' )
from emp_table;
Values in a timestamp (or date, time, integer or any type non-character type) are not stored in any specific format.
Any format you see is applied by the application you are using to display the values - typically the SQL client you are using.
There are two ways to change that:
Configure your SQL client to use a different timestamp format for display (how you do that depends on the SQL client you are using - check its manual)
Use the the_char() function to format your timestamp value throug SQL
select to_char(the_column, 'yyyy-mm-dd hh24:mi:ss')
from the_table
More details on the available formats can be found in the manual: https://www.postgresql.org/docs/current/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE
Easily, just cast it like follows:
SELECT '2016-07-01 01:12:22 PM'::timestamp;
timestamp
---------------------
2016-07-01 13:12:22
(1 row)

Postgresql date inconsistent format

I am building an app using node with a Postgres backend. I have a column for date where the data type is date. The dates in the database are stored in the format YYYY-MM-DD. However, when I do a query in my app, the date returned is in the format YYYY-MM-DD + (time zone information). For example:
2016-01-06T05:00:00.000Z
Does anyone know how I can prevent this?
Thanks in advance!
Use to_char
SELECT to_char(dateColumn, 'YYYY-MM-DD') AS formattedDate FROM ...
Example:
ubuntu=> SELECT to_char(to_timestamp('2016-01-07', 'YYYY-MM-DD'), 'YYYY-MM-DD');
to_char
------------
2016-01-07
(1 row)