PostgreSQL : cast string to date DD/MM/YYYY - string

I'm trying to cast a CHARACTER VARYING column to a DATE but I need a date format like this : DD/MM/YYYY. I use the following SQL query :
ALTER TABLE test
ALTER COLUMN date TYPE DATE using to_date(date, 'DD/MM/YYYY');
The result is a date like this : YYYY-MM-DD.
How can I get the DD/MM/YYYYformat ?
Thanks a lot in advance !
Thomas

A DATE column does not have a format. You cannot specify a format for it.
You can use DateStyle to control how PostgreSQL emits dates, but it's global and a bit limited.
Instead, you should use to_char to format the date when you query it, or format it in the client application. Like:
SELECT to_char("date", 'DD/MM/YYYY') FROM mytable;
e.g.
regress=> SELECT to_char(DATE '2014-04-01', 'DD/MM/YYYY');
to_char
------------
01/04/2014
(1 row)

https://www.postgresql.org/docs/8.4/functions-formatting.html
SELECT to_char(date_field, 'DD/MM/YYYY')
FROM table

The documentation says
The output format of the date/time types can be set to one of the four
styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date
format), or German. The default is the ISO format.
So this particular format can be controlled with postgres date time output, eg:
t=# select now();
now
-------------------------------
2017-11-29 09:15:25.348342+00
(1 row)
t=# set datestyle to DMY, SQL;
SET
t=# select now();
now
-------------------------------
29/11/2017 09:15:31.28477 UTC
(1 row)
t=# select now()::date;
now
------------
29/11/2017
(1 row)
Mind that as #Craig mentioned in his answer, changing datestyle will also (and in first turn) change the way postgres parses date.

In case you need to convert the returned date of a select statement to a specific format you may use the following:
select to_char(DATE (*date_you_want_to_select*)::date, 'DD/MM/YYYY') as "Formated Date"

Depends on which type you require as output, but here are 2 quick examples based on an intervals:
SELECT (now() - interval '15 DAY')::date AS order_date -> 2021-07-29
SELECT to_char(now() - interval '15 DAY', 'YYYY-MM-DD') -> 2021-07-29

Let's say your date column is order_date:
SELECT (
RIGHT(order_date, 4)
|| '-'
|| SUBSTRING(order_date, 4, 2)
|| '-'
|| LEFT(order_date, 2)
)::DATE
FROM test
OR
SELECT CAST(
RIGHT(order_date, 4)
|| '-'
|| SUBSTRING(order_date, 4, 2)
|| '-'
|| LEFT(order_date, 2)
AS DATE )
FROM test

Related

How to convert Persian (Shamsi) date to Gregorian (Miladi) date with Function Script in Excel or Google Sheet?

Does anyone know how to convert a Persian date to a Gregorian date using an Excel / Google Sheet function?
for example:
1401/06/06
to:
2022/08/28
P.S: Earlier I found a function to convert Gregorian to Jalali written by Amir Fo, But my question is about converting from Persian (Shamsi) to Gregorian.
This can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
Select some cell in your Data Table
Data => Get&Transform => from Table/Range or from within sheet
When the PQ Editor opens, over on the right under Applied Steps, the second step will be #"Changed Type"
Edit that step to add fa-IR as the culture
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}} , "fa-IR")
This is the same as Changed Type with locale using Persian as the locale
try:
=TEXT(VLOOKUP("Gregorian calendar", IMPORTHTML("https://date-today.com/en/shamsi-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)\/")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\d{4}")*1&"-to-gregorian-calendar.html", "table", 1), 2, ),
"e/mm/dd")
or:
=TEXT(VLOOKUP("Gregorian calendar", IMPORTHTML("https://date-today.com/en/shamsi-"&
REGEXEXTRACT(TO_TEXT(A1), "\d+")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)\/")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\d{4}")*1&"-to-gregorian-calendar.html", "table", 1), 2, ),
"yyyy/mm/dd")
REVERSE:
Convert date locale in google sheet from Gregorian calendar to Jalali calendar
Here is a pure Excel solution, which does not require an internet connection or Power Query.
Disclaimer: I've found the function here and made some modifications to make it more readable. BTW, I've tested the function for the period 1396/07/01 to 1401/07/01 and it works OK, but use it with extensive testing and at your own risk.
Basically, the function calculates the total days since 1278/10/11 Shamsi (which is equal to the Excel date origin, 1900-01-01). The result of the function is an integer (date value).
Below, you will find the solution in two formats, one using the new LET function (for Excel 2021 and Office 365) and one without:
Note that the Shamsi date should be in the format 1401/07/21.
Using LET:
=LET(
y,VALUE(LEFT(A1,4)),
m,VALUE(MID(A1,6,2)),
d,VALUE(RIGHT(A1,2)),
full_months,IF((m-1)<7,(m-1)*31,IF((m-1)>6,(m-1)*30+6)),
total,(y-1)*365+full_months+d+INT((y-1)/4),
IF(MOD(y,4)=0,total+1,total)-466710
)
Legacy version:
IF(
MOD(VALUE(LEFT(A1,4)),4)=0,
(VALUE(LEFT(A1,4))-1)*365+(
IF(
(VALUE(MID(A1,6,2))-1)<7,
(VALUE(MID(A1,6,2))-1)*31,
IF(
(VALUE(MID(A1,6,2))-1)>6,
(VALUE(MID(A1,6,2))-1)*30+6
)
)
)+VALUE(RIGHT(A1,2))+INT((VALUE(LEFT(A1,4))-1)/4)+1,
(VALUE(LEFT(A1,4))-1)*365+(
IF(
(VALUE(MID(A1,6,2))-1)<7,
(VALUE(MID(A1,6,2))-1)*31,
IF(
(VALUE(MID(A1,6,2))-1)>6,
(VALUE(MID(A1,6,2))-1)*30+6
)
)
)+VALUE(RIGHT(A1,2))+INT((VALUE(LEFT(A1,4))-1)/4)
)-466710
To convert the resulting integer to date, either change the cell format to Date or use the following formula:
TEXT(A2,"yyyy-mm-dd")
Microsoft Excel supports all calendar conversion.
The key is to follow procedures one by one.
suppose We want to convert cell A1=10/10/1401 from Persian calendar to English Calendar and set the result in the Cell B1
Step 1:
Right Click on B1 > Format cells... from Number tab in the category select Date and change it to desire calendar type, in this case English.
For B1 also type the formula =A1.
Step 2:
Right click on A1 > Format cells... from Number tab in the category select Date and change the calendar type to whatever your date type is; in this case Persian.
Tick the check mark input dates according to selected calendar
Step 3:
Enter your Persian date in cell A1. You'll get the converting date in cell B1.
Be aware of the order you enter the year and month and the day.

HIVE where date filter by x days back? string format

so our DBA's setup our hive table with the date column as the partition column, but as a "string" YYYYMMDD format.
How can I WHERE filter this "date" column for something like last 30 days?
Please use date_format to format systemdate - 30 days into YYYYMMDD and then compare with your partition column. Please note to use partition column as is so hive can choose correct partitions.
When you want to pick previous 30th days data -
select *
from mytable
where partition_col = date_format( current_date() - interval '30' days, 'yyyyMMdd')
If you want all data since last 30 days -
select *
from mytable
wherecast(partition_col as INT) >= cast(date_format( current_date() - interval '30' days, 'yyyyMMdd') as INT)
casting shouldnt impact partition benefits but you need to check the performance before using it. Please get back in such scenario.

Retrieve rows from last 24 hours

I have a table with the following (with other fields removed)
CREATE TABLE if NOT EXISTS request_audit (
user_id text,
request_body text,
lookup_timestamp TIMESTAMP
PRIMARY KEY ((user_id), lookup_timestamp)
) WITH CLUSTERING ORDER BY ( lookup_timestamp DESC);
I create a record with the following
INSERT INTO request_audit (user_id, lookup_timestamp, request_body) VALUES (?, ?, toTimestamp(now()))
I am trying to retrieve all rows within the last 24 hours, but I am having trouble with the timestamp,
I have tried
SELECT * from request_audit WHERE user_id = '1234' AND lookup_timestamp > toTimestamp(now() - "1 day" )
and various other ways of trying to take a day away from the query.
Cassandra has a very limited date operation support. What you need is a custom function to do date math calculation.
Inspired from here.
How to get Last 6 Month data comparing with timestamp column using cassandra query?
you can write a UDF (user defined function) to date operation.
CREATE FUNCTION dateAdd(date timestamp, day int)
CALLED ON NULL INPUT
RETURNS timestamp
LANGUAGE java
AS
$$java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
c.add(java.util.Calendar.DAY_OF_MONTH, day);
return c.getTime();$$ ;
remember that you would have to enable UDF in config. Cassandra.yml. Hope that is possible.
enable_user_defined_functions: true
once done this query works perfectly.
SELECT * from request_audit WHERE user_id = '1234' AND lookup_timestamp > dateAdd(dateof(now()), -1)
You couldn't do it directly from CQL, as it doesn't support this kind of expressions. If you're running this query from cqlsh, then you can try to substitute the desired date with something like this:
date --date='-1 day' '+%F %T%z'
and execute this query.
If you're invoking this from your program, just use corresponding date/time library to get date corresponding -1 day, but this depends on the language that you're using.

Parsing non iso datetime string to just date part in presto

I have table which stores datetime as varchar
Format looks like this 2018-07-16 15:00:00.0 ,
I want to parse this to extract only date part so that I use date part to compare with date in string format such as '2018-07-20' in where clause. What is the best way to achieve this in presto?
This particular format (based on example value 2018-07-16 15:00:00.0 in the question) is understood by cast from varchar to timestamp. You then need to extract date part with another cast:
presto> SELECT CAST(CAST('2018-07-16 15:00:00.0' AS timestamp) AS date);
_col0
------------
2018-07-16
(1 row)

To_char function in databricks

I am using sql as language for my notebook in databricks.
Want to get the day of week from the date given.
For doing this i used to_char(date,'fmday'). Getting error as function is not registered as temporary or permanant in databricks. Is there a way to get the name of day by other means.
Date is in format yyyymmdd
You are getting that error because to_char is not a SparkSQL function. You can see the list of functions in the ScalaDocs here: https://spark.apache.org/docs/latest/api/scala/org/apache/spark/sql/functions$.html
If your date is a DateType, you can do dayofweek(date) in SparkSQL.
get the name of the day
Being as you want to get the name of the day, you can use the date_format function with the argument 'EEEE' to get the day name, eg Monday. If you want to pass in an integer (eg numbers between 1 and 7) then you could just code a CASE statement, something like:
%sql
SELECT
dayofweek( CAST( '2018-12-31' AS DATE ) ) AS d,
date_format( CAST( '2018-12-31' AS DATE ), 'EEEE' ) AS dayname,
CASE dayofweek( CAST( '2018-12-31' AS DATE ) )
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'Unknown'
END AS caseTest
NB I have coded the CASE to start the week from Day 1 - Monday, which is different to the dayofweek default; this might be one reason to do that, ie you want a different default.
My Results:
I got a way to get the name of day of week as below
date_format(to_date('20170821','yyyyMMdd'),'EEEE')
Now i want to pass a column of integer datatype, but when i pass it to query getting null as output. Could someone please help

Resources