PostgreSQL convert month name to number - postgresql

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

Related

How to fetch only year from date in postgresql table

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.

how to get hour, month from timestamp in postgresql

timestamp with timezone is this - 2020-05-31T10:05:07Z
this is not working, despite referencing official documentation. I need to extract may 2020 or separate month and year to compare against May 2020
SELECT date_trunc('hour', TIMESTAMP '2020-05-31T10:05:07Z')
SELECT date_part('day', TIMESTAMP '2020-05-31T10:05:07Z');
If you want to check if a timestamp value is "may 2020", you have different options.
to_char(the_value, 'yyyy-mm') = '2020-05'
or
extract(month from the_value) = 5
and extract(year from the_value) = 2020
or
(extract(month from the_value), extract(year from the_value)) = (5, 2020)
extract() and date_part() are the same thing - but I prefer the standard compliant extract() version.
demo:db<>fiddle
You need to_char() to format a date or timestamp. Mon gives you the first three letters of a month name:
SELECT
to_char(
TIMESTAMP '2020-05-31T10:05:07Z',
'Mon YYYY'
)
Returning the entire month name you can use Month instead of Mon. But, for some reasons, the length of the Month value is fixed to the longest month name available. That means May is returned with right padded spaces. To avoid this, you need to add the modifier FM:
SELECT
to_char(
TIMESTAMP '2020-05-31T10:05:07Z',
'FMMonth YYYY'
)

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')

How to get DateTime formatted

In sql server how can I get current date but time 12:00 AM.
With GETDATE() i get current date and time.I need to have datetime formated like this:
2011-02-04 12:00 AM OR 2011-02-04 00:01
you first need to know how to get the time you want. It is called "flooring" date time. see this example:
--Floor a datetime
SELECT '0 None', GETDATE() -- none 2008-09-17 12:56:53.430
UNION SELECT '1 Second',DATEADD(second,DATEDIFF(second,'2000-01-01',GETDATE()),'2000-01-01') -- Second: 2008-09-17 12:56:53.000
UNION SELECT '2 Minute',DATEADD(minute,DATEDIFF(minute,0,GETDATE()),0) -- Minute: 2008-09-17 12:56:00.000
UNION SELECT '3 Hour', DATEADD(hour,DATEDIFF(hour,0,GETDATE()),0) -- Hour: 2008-09-17 12:00:00.000
UNION SELECT '4 Day', DATEADD(day,DATEDIFF(day,0,GETDATE()),0) -- Day: 2008-09-17 00:00:00.000
UNION SELECT '5 Month', DATEADD(month,DATEDIFF(month,0,GETDATE()),0) -- Month: 2008-09-01 00:00:00.000
UNION SELECT '6 Year', DATEADD(year,DATEDIFF(year,0,GETDATE()),0) -- Year: 2008-01-01 00:00:00.000
ORDER BY 1
PRINT' '
PRINT 'Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor'
PRINT 'this always uses a date less than the given date, so there will be no arithmetic overflow'
SELECT '1 Second',DATEADD(second,DATEDIFF(second,DATEADD(day,DATEDIFF(day,0,GETDATE()),0)-1,GETDATE()),DATEADD(day,DATEDIFF(day,0,GETDATE()),0)-1) -- Second: 2008-09-17 12:56:53.000
once you floor it, use one of the flavors of CONVERT() to format it as you would like:
this does the format you want, but without changing the time:
select CONVERT(char(10), GETDATE(), 121)+' '+LTRIM(RIGHT(CONVERT(varchar(30), GETDATE(), 100),7))
OUTPUT:
------------------
2011-02-04 7:19AM
to format and set the time to what you want:
select CONVERT(char(10),DATEADD(day,DATEDIFF(day,0,GETDATE()),0), 121)+' '+LTRIM(RIGHT(CONVERT(varchar(30), DATEADD(day,DATEDIFF(day,0,GETDATE()),0), 100),7))
OUTPUT:
------------------
2011-02-04 12:00AM
Check out the CAST and CONVERT functions in T-SQL - they allow you to format your DATETIME values in various ways.
There are a number of Date functions you can use in SQL Server - See here.
Hopefully these will help!
To simply set the time to 12:00 AM but maintain the datetime datatype try:
SELECT DATEADD(DAY,0,DATEDIFF(DAY,0,GETDATE()))