Postgresql data value only years - postgresql

Is it possible to set value of onl year(without months and days) like 2005, 2001 in Postgresql if data type is Date?
Or should I use other variable types?
How can I set default month/day in postgres?

you can't set a part of a date individually. You could however do something like this:
update the_table
set the_date_column = to_date('2005'||to_char(the_date_column, 'mmdd'), 'yyyymmdd');

You can do something like this:
SELECT to_date('2005','yyyymmdd')
but if you want to store only year than integer is more suitable and preferred.

No, that would cause syntax error:
select date '2014';
-- ERROR: invalid input syntax for type date: "2014"
-- SQL state: 22007
You can use 1st of january in all of your inputs, but if you want to store only the year, integer is more suitable.

Related

PostgreSQL TO_DATE date/time field value out of range

I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.

postgreSQL increment number in output

I am extracting three values (server, region, max(date)) from my postgresql> But I want to extract an additional 4th field which should be the numerical addition of 1 to 3rd field. I am unable to use date add function as in the database date field is defined as an integer.
date type in DB
date|integer|not null
tried using cast and date add function
MAX(s.date)::date + cast('1 day' as interval)
Error Received
ERROR: cannot cast type integer to date
Required output
select server, region, max(alarm_date), next date from table .....
testserver, europe, 20190901, 20190902
testserver2, europe, 20191001, 20191002
next date value should be the addition to alarm_date
To convert an integer like 20190901 to a date, use something like
to_date(CAST(s.date AS text), 'YYYYMMDD')
It is a bad idea to store dates as integers like that. Using the date data type will prevent corrupted data from entering the database, and it will make all operations natural.
First solution that came to my mind:
select (20190901::varchar)::date + 1
Which output 2019-09-02 as type date.
Other solutions can be found here.

Converting inconsistent strings to date

I'm working with a data set that has a date column in the form of string. I thought it would a simple by doing something like this:
to_date(date_of_birth,'YYYY-01-01')
date_of_birth is in the format DD/MM/YYYY, and is of type 'text'
HOWEVER, I stumbled upon some crazy cases where you have information like
//1980
Another case was:
0/0/1980
When I run my solution, I receive the following error:
ERROR: invalid value "//19" for "YYYY"
DETAIL: Value must be an integer.
My goal actually is simply to collect the year, since that's at least consistent. How do you handle such cases with Postgres?
EDIT:
Switched it to the following:
to_date(date_of_birth,'01/01/YYYY')
My query is this:
SELECT to_date(date_of_birth,'01/01/YYYY') AS year, COUNT(*) AS yearTotal FROM student WHERE date_of_birth LIKE '%/%/1980' GROUP BY year;
The result turns out like this:
year | yeartotal
---------------+-----------
0030-01-01 | 3
0001-01-01 BC | 1
The problem through manipulating the date information (which was of type string) into 'YYYY' in order to access the proper information through date_part.
Using PostgreSQL's "right" function, I was able to collect the year from the string written in the format "DD/MM/YYYY", where right('10/12/2013',4) will return '2013'.
My query looked like this:
SELECT date_part('year', to_date(right(date_of_birth,4),'YYYY')) AS year, COUNT(*) AS total FROM student GROUP BY year
Another way I could've solved the problem was through regex by ensuring that I only work with 'valid' date statements. Something along the line of (using Python regex as an example):
regexp_matches(date_of_birth, '.\/.\/\d{4}')
try TO_CHAR instead of TO_DATE, it worked for me
SELECT replace(date_of_birth,'//','') AS year, COUNT(*) AS yearTotal FROM student GROUP BY year;

Conversion failed when converting date and/or time from character string Error

Select CONVERT(Date, '13-5-2012')
When i run the above T-SQL statement in Management Studio, i get i get the following error:
"Conversion failed when converting date and/or time from character string"
Is there away i can cast that value to a valid Date type successfully? I have such values in a nvarchar(255) column whose dataType i want to change to Date type in an SQL Server table but i have hit that error and i would like to first do a conversion in an Update statement on the table.
Specify what date format you are using:
Select CONVERT(Date, '13-5-2012', 105)
105 means Italian date format with century (dd-mm-yyyy).
Ref: http://msdn.microsoft.com/en-us/library/ms187928.aspx
In general, I'd suspect usually there is data which can't be converted in a column, and would use a case statement checking it's convertable first:
SELECT CASE WHEN ISDATE(mycolumn)=1 THEN CONVERT(Date, mycolumn, [style]) END
FROM mytable
I believe Convert relies on the SQL Server date format setting. Please check your dateformat setting with DBCC USEROPTIONS.
I suspect if you set the dateformat to dmy it'll understand:
SET DATEFORMAT dmy
GO
If even then it doesn't work, you can't find a style that matches your data, and if your data is in a consistant format, it's down to manual string manipulation to build it (don't do this if you can help it).
Try this....
Select CONVERT(Date,'5-13-2012')
Use 'mm-dd-yyyy' format.
CONVERT assumes that the original data can represent a date. One bad data item can throw the same conversion error mentioned here without pointing to the problem.
Using ISDATE helped me get around the bad data items.
SELECT CONVERT(DATE, CONVERT(CHAR(8), FieldName))
FROM DBName
WHERE ISDATE(FieldName) <> 0
You need to give the date format while conversion, this will resolve the error.
select convert(date, '13-5-2012' ,103)

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.