How to fetch only year from date in postgresql table - postgresql

I have a table like this in postgresql:
Name
DOB
ABC
'2011-03-03'
XYZ
'2009-01-01'
What is the query that I should use to get the output data in the below format(only year instead of date) also I want to retrieve data that is greater than 2010 only:
Name
DOB
ABC
'2011'

Format DOB using to_char.
select "Name", to_char(DOB, 'yyyy') DOB
from the_table
where extract('year' from DOB) > 2010;
If DOB is character rather than date type then it has to be first cast to date:
select "Name", to_char(DOB::date, 'yyyy') DOB
from the_table
where extract('year' from DOB::date) > 2010;
If your date represented as text has "month/day/year" format then use to_date to convert it to date.
select "Name", to_char(to_date(DOB, 'mm/dd/yyyy'), 'yyyy') DOB
from the_table
where extract('year' from to_date(DOB, 'mm/dd/yyyy')) > 2010;
Unrelated but do not store dates as formatted text. You have data type date for this.

Related

Create date column from year and doy column

Is there a way to create a date column combining one column having the year as string and one column containing a date-of-year (doy) as integer?
I am aware of methods like SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); or SELECT to_char(date_trunc('year', now()) + interval '169 days', 'MM/DD') but when trying to replace the "hard coded" stings with the columns I always get some kind of an error.
SELECT s.id, s.year, s.doy,
((s.year||'-01-01')::date + (s.doy||' days')::interval )::date AS date
FROM table_name AS s
the (s.year||'-01-01') or (s.doy||' days') concats the column value with a required string and the ::date or ::interval changes the resulting string type
You can use the make_date() function and add the number of days directly because date + integer is a valid operation:
select make_date(s.year, 1, 1) + s.doy as date
from ...

Convert String "Friday June 1, 2018" to Date

I have a column in database from imported file in the format Friday June 1, 2018 and trying to format to a valid date conversion from varchar to date.
I have tried cast and convert with no success
SELECT CAST([Course date] as DATETIME)
FROM [dbo].[Test]
SELECT CONVERT(datetime, [Course date], 103)
FROM [dbo].[Test]
I expected conversion to type 103 UK dd/mm/yyyy
TRY_CONVERT seems to be able to handle your date string. Note that the name of the day is superfluous, and is not needed to determine exactly what the date is. So, we can remove it using base string functions.
WITH yourTable AS (
SELECT 'Friday June 1, 2018' AS datecol
)
SELECT
datecol AS input,
TRY_CONVERT(datetime, STUFF(datecol, 1, CHARINDEX(' ', datecol), '')) AS output
FROM yourTable;
Demo

I need to get 3 letter month as 'Jan', 'Feb' using extract () function from date column in postgresql

date column's format : '2017-03-17'
SELECT EXTRACT(MONTH FROM DATE '2017-03-17') FROM table;
It's giving me 3 instead of March.
extract() returns a number. You want a string, so you can use to_char(<col>, 'Mon'):
select to_char(now(), 'MON')
or for mixed case:
select to_char(now(), 'Mon')

PostgreSQL convert month name to number

I have a query that returns a field as month and year for example "Nov 2015" I want to get the number for the month, here I have to get number as "11".
You can use the to_date function that can parse input in a certain format into a 'real' date. Then with this date you can extract the month from it.
select EXTRACT(MONTH FROM to_date('Nov 2015', 'Mon YYYY'))
See http://www.postgresql.org/docs/current/static/functions-formatting.html for the formatting syntax and http://www.postgresql.org/docs/current/static/functions-datetime.html for the other datetime functions etc..
to_char()
select to_char(to_date('Nov 2015', 'Mon YYYY'), 'mm') month_no
and the query can be
select to_char(to_date(your_date_col, 'Mon YYYY'), 'mm') month_no
from table_name
If your date is timestamp , for example:"2017-01-24 23:00:00-03" (this is timestamp with time zone)
select to_char(your_date, 'MM') from your_table
Then the result would be in this case: "01"
You can use the function to_timestamp(text, text) and extract(field from timestamp):
SELECT EXTRACT(MONTH FROM TO_TIMESTAMP("Nov 2015", "Mon YYYY"));
see the documentation for to_timestamp and extract

Select lines whose date-field is in a given month and year

My SQL table looks like this:
id (int) | date (date) | text1 (varchar) | text2 (varchar)
I want to select the lines whose date suits a given month and year, regardless of the day.
Both month and year are given in the select-statement as integers.
So the missing thing is the where-clause. Perhaps extract() is the thing I'm looking for, but I don't know how to use it with the two integers, e.g. 2011 and 02.
You can use extract:
SELECT * FROM yourtable
WHERE EXTRACT(month FROM "date") = 2
AND EXTRACT(year FROM "date") = 2011
But in this case you could also do this:
SELECT * FROM yourtable
WHERE "date" >= '2011-02-01' AND "date" < '2011-03-01'