YEAR gives error - sql

pgAdmin III always gives an error on year
ERROR: function year(date) does not exist
LINE 1: SELECT YEAR(geboortedatum) as date_part
.............................^^^^.........
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function year(date) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 8
I can get the whole date that I need but I need only the year can someone help me it needs to be the youngest year from the table.

As you mentioned pgAdmin, I assume that you are using Postgres, so you need write code like this (using EXTRACT constructure):
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
Detailed documentation for the datetime functions

Related

SQL: function trunc(timestamp without time zone, "unknown") does not exist

I am trying to transform a date column to week by using the following code:
select trunc(join_date, 'D') as join_wk from my_table
But I got the following errors:
function trunc(timestamp without time zone, "unknown") does not exist
Hint: No function matches the given name and argument types. You may need to add explicit type casts.
where join_date is of the format:
2017-08-24 14:49:59
What did I do wrong in the query? Thanks!
The name of the function is date_trunk, you have to swap the parameters:
SELECT DATE_TRUNC('D', join_date) AS join_wk FROM my_table

Convert date to specific format using Postgresql

I am needing to convert records where the TEXT values look like this using Postgresql:
26-AUG-2015
to:
2015-08-26
I'm not sure what version of Postgresql exists on the vendor server but I tried to do a select statement using:
SELECT to_char(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entires sle;
But I'm getting this error:
Error: SQL Error: SQLSTATE[42883]: Undefined function: 7 ERROR: function to_char(text, unknown) does not exist LINE 25: AND to_char(sle.log_field1, 'YYYY-MM-DD') >=... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I did also try:
SELECT to_date(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entries sle
But I got this error:
Error: SQL Error: SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid value "[E] " for "YYYY" DETAIL: Value must be an integer. Query: SELECT to_date(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entries sle
Any suggestions/direction would be appreciated. Thanks.
This assumes that lc_time is set to English:
SELECT to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD');
to_char
------------
2015-08-26
(1 row)
You can convert the value to a date and then back to a string:
select to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
Under many circumstances, processing the value as a date is probably sufficient.
Your approach doesn't work because the value is apparently already stored as a string, so converting it back to a string with a date format doesn't make sense.
EDIT:
You may be able to get by using a simple regular expression:
select (case when col ~ '^[0-9]{2}[-][A-Z]{3}[-][0-9]{4}$'
then to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
end)
Of course, if the formatting errors are more subtle, then a more complex regular expression would be needed.
try this,
SELECT to_char(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entires sale;

How to obtain records of a specific month in SQL by date

I'm using Postgres 9.1 and try to get records from a table knowing the given month and should know if the date type date field contains the month.
This is my query:
SELECT consejo.numero,consejo.fecha FROM consejo WHERE MONTH(consejo.fecha) = 2
but I get the following error:
ERROR: There is no month function (date)
LINE 1: ... T consejo.numero, consejo.fecha council FROM WHERE MONTH (cons ...
HINT: No function matches the name and types of arguments. You may need to add explicit type conversion.
might be wrong?
In Postgresql there is no MONTH function available. You can use EXTRACT instead:
WHERE EXTRACT(MONTH FROM consejo.fecha) = 2
Another option is to use PostgreSQL's date_part() function:
WHERE date_part('month', consejo.fecha) = 2
Look at the reference guide:
http://www.postgresql.org/docs/9.5/static/functions-datetime.html

Date Manipulation in Informix DB

based on other forum entries, I believe that I need to utilise the below code to convert a date in Informix to a month:
select
MONTH(DATE_FIELD) as "Month Conversion"
from TABLE
Unfortunately when I run it, I get this error
Error: ERROR Mixed numeric and alpha operands in expression -
job_cost_master.jcm_start_date SQLState: 42000 ErrorCode: -1
You cannot use a column ALIAS with spaces and/or in quotes.
This should work:
select
MONTH(DATE_FIELD) as month_conversion
from TABLE
Regards

Error in Casting date postgreSQL

I'm trying to cast a varchar into a date with this following code, and the following error is outputting, any idea why?
ALTER TABLE import.employee
ALTER COLUMN birth_date
TYPE date
USING (birth_date::date);
ERROR: date/time field value out of range: "05/29/1960"
HINT: Perhaps you need a different "datestyle" setting.
********** Error **********
ERROR: date/time field value out of range: "05/29/1960"
SQL state: 22008
Hint: Perhaps you need a different "datestyle" setting.
Set the datestyle before:
set datestyle = mdy;
If you need to get the ::date from birth_date, first give the DATE format to your field using the given functions in Data Type Formatting Functions docs.