How to add only date like 'DD/MM/YYYY' to timestamp in PostgreSQL? - postgresql

I'm trying to add only the current date in "DD/MM/YYYY" format in a field of type ' timestamp in PostgreSQL' .
I try:
select to_char(now(),'DD/MM/YYYY') as date;
But PostgreSQL return me:
TIP : You will need to rewrite the expression or apply a type conversion.

There is no such thing as "only the date" in a timestamp field. A timestamp field will store timestamps.
Try using the date type instead. Please read about this here.
Also, please consider using the ISO 8601 format instead. Getting used to it helps in a lot of cases.
Human-readable formats like "DD/MM/YYYY" should only be used for presentation.
If you want to use timestamp fields and insert a human-readable formatted dates, then you are looking for:
to_timestamp('05/04/2016', 'DD/MM/YYYY')
If it is about the current date, then Postgres provides the CURRENT_DATE function, which you may use:
SELECT CURRENT_DATE;
INSERT INTO t (timestamp_field) VALUES (CURRENT_DATE);

Related

Date formatting in postgresql from existing data

I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')

PostgreSQL Date format issue

The Postgres Query select date('22-02-2022')
is showing Error message.
I need to save date in a column of a table, but the input date can be any format. Please suggest how to save date in YYYY-MM-DD format.
Following is the Error screenshot:
A date has no format at all. But if you specify a date literal (constant) the way you tried, it has to be in yyyy-mm-dd format:
select date '2022-02-22'
If you want to specify the value in a different format, use the to_date() function:
select to_date('22-02-2022', 'dd-mm-yyyyy');
If the column in the table is defined with the date data type, the way you specify the actually value is irrelevant as it will be stored as a binary value without any format.
If you need a specific format when selecting (displaying) those values, you can use the to_char() function.
If all your dates and timestamps are going to come in this way then as the HINT suggests you need to change the DateStyle.
Run SHOW DateStyle, I'm guessing it will return something like ISO, MDY.
Change it SET DateStyle = 'ISO, DMY', then your example will work.
Example:
show datestyle ;
DateStyle
-----------
ISO, MDY
select date('22-02-2022');
ERROR: date/time field value out of range: "22-02-2022"
LINE 1: select date('22-02-2022');
^
HINT: Perhaps you need a different "datestyle" setting.
set datestyle = 'ISO,DMY';
select date '22-02-2022';
date
------------
2022-02-22

Change date format with PostgreSQL (AWS Redshift) from 'M/D/YYYY' to 'DD/MM/YYYY' for Qlik Sense date picker extension

I am trying to change the format of a timestamp field to a date field in a certain format with AWS Redshift.
I googled a lot and the common "best practice" that I found was to cast the timestamp to a date and then use to_char to bring it into the right format. In the end I want to use the date field in a Qlik Sense dashboard where it is input for a date picker extension that apparently requires the format DD/MM/YYYY to work.
Current date format in the DB:
9/2/2019 6:38:00 AM (which I would describe as M/D/YYYY H:MM:SS ZZ)
Desired output:
DD/MM/YYYY, resulting in the value 02/09/2019
Current status:
to_char(cast(timestamp_field as date), 'DD/MM/YYYY') --> result: 02/09/2019
However, the date picker extension in Qlik Sense still does not work and I guess that's because the output is a string and not a date. Casting the string to a date returns in an error.
cast(to_char(cast(timestamp_field as date), 'DD/MM/YYYY') as date) as date_picker_date
Connector reply error: SQL##f - SqlState: 57014, ErrorCode: 30,
ErrorMsg: [Amazon][Amazon Redshift] (30) Error occurred while trying
to execute a query: [SQLState 57014] ERROR: Error converting text to
date
I am new to Redshift and would have expected to be able to cast to date with a format string as parameter, but apparently that's not a thing. Can someone enlighten me on how to solve this?
You can try with the timestamp at the beginning of the script of the QlikSense.
It will be at the start of the script. in the Main section as shown below:
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';
here SET is being used to define the time stamp in that application.
You can try changing here to your requirements.
Else 2 options 1 use a stamp which you have already used or create your own extension from the lib
Stamp use can be like this:
Datestamp_Table:
Load *,
Date(Date#(StringDate,'M/D/YY'),'DD/MMM/YY') as Date;
LOAD * INLINE [
StringDate
8/7/97
8/6/97];
Output will be like this:
Stringdate Date
8/7/97 07/Aug/97
8/6/97 06/Aug/97

Obtain date without timestamp in DB2

Please pardon my ignorance if I have missed any documentation/solution for the same. But I searched the web and could not find an answer.
I have a simple question. In the DB2 table,I have a column of type date and the with data of format 04/25/2013 12:00:00AM . When I query the DB2 database, I want to obtain just the date and not the timestamp i.e to obtain "04/25/2013" and not "04/25/2013 12:00:00AM". I tried DATE(column name) and just gave back the complete value including the time stamp.
This looks like a TIMESTAMP and not a DATE column. If it is indeed a TIMESTAMP column try this:
select varchar_format(current timestamp, 'MM/DD/YYYY') from sysibm.sysdummy1 ;
Just replace the current timestamp in the above example with your column and sysibm.sysdummy1 with your table.
The good thing about varchar_format is that it lets you easily format the timestamp. Just change the 'MM/DD/YYYY' part to 'YYYY.MM.DD' to get a format like '2017.08.18'.

How to convert d/MM/yyyy data to dd/MM/yyyy in sql server table?

I have create one field in sql server database as nvarchar datatype and store some date like 'd/MM/yyyy' and 'dd/MM/yyyy' format previously. Now i want to get all data in 'dd/MM/yyyy' format using query it is possible?
You can cast the field to datetime in the query:
select cast(YourField as datetime)
from YourTable
where isdate(YourField) = 1
The where isdate(YourField) = 1 part is necessary to filter out rows where the value is no valid date (it's a nvarchar field, so there could be things like abc in some rows!)
But you should really change the field to datetime in the long term, as already suggested by Christopher in his comment.
Casting like described above is always error-prone because of the many different data formats in different countries.
For example, I live in Germany where the official date format is dd.mm.yyyy.
So today (December 9th) is 9.12.2011, and running select cast('9.12.2011' as datetime) on my machine returns the correct datetime value.
Another common format is mm/dd/yyyy, so December 9th would be 12/9/2011.
Now imagine I have a nvarchar field with a date in this format on my German machine:
select cast('12/9/2011' as datetime) will return September 12th (instead of December 9th)!
Issues like this can easily be avoided by using the proper type for the column, in this case datetime.