Postgres quarter function - postgresql-9.4

When running
select to_char ("dateoftransfer", '"Q"quarter YYYY') as quarterly
FROM table;
I get back
Q3uarter 2001 but expect Q3 2001
"dateoftransfer" is data type date
What am i doing wrong?

I think you are looking for this format:
select to_char(dateoftransfer, '"Q"Q YYYY')

Related

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

converting milliseconds to string date dd MMM YYYY and using it in LIKE Clause

I have stored a date in milliseconds in table as follow:
table: person
columns: id,name,dob
want to select person details based on given dob
Eg. SELECT id,name,to_date(dob) as dob FROM person WHERE dob LIKE '10 Jun 1991'
here function to_date() should select the milliseconds and convert to format '10 Jun 1991'
You can convert your timestamp in milliseconds to the desired format with the following if the milliseconds are measured from the UNIX epoch on:
to_char(to_timestamp(dob / 1000.0), 'DD Mon YYYY')
For MySQL
SELECT id, name, FROM_UNIXTIME(dob / 1000) FROM person
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
For Postgres:
SELECT id, name, to_timestamp(dob / 1000) FROM person
https://www.postgresql.org/docs/current/static/functions-formatting.html

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

Getting Dates by Selecting a week in oracle

I have a textbox with random numbers from 1 to 52 which are week numbers of a calendar and a drop down which mentions as years.
For example if I select 2 in a textbox with year 2014, then I want the dates to be mentioned as 05-1-2014 - 11-1-2014. Is it possible to do it.
Also I have tried one query which doesnt match my requirement
SELECT date_val, TO_CHAR (date_val, 'ww')
FROM (SELECT TO_DATE ('01-jan-2013', 'DD-MON-YYYY') + LEVEL AS date_val
FROM DUAL
CONNECT BY LEVEL <= 365)
Please help.
Try this. Here 2 is the number of week in the year (FirstSunday+(NumberOfWeek-1)*7 as WeekStart, FirstSunday+ NumberOfWeek*7-1 as WeekEnd) and 2014 is a year:
select
FirstSunday+(2-1)*7 as WeekStart,
FirstSunday+ 2*7-1 as WeekEnd
from
(
Select NEXT_DAY(TO_DATE('01/01/'||'2014','DD/MM/YYYY')-7, 'SUN') as FirstSunday
from dual
)
SQLFiddle demo
Try this too,
SELECT start_date,
start_date + 6 end_day
FROM(
SELECT TRUNC(Trunc(to_date('2014', 'YYYY'),'YYYY')+ 1 * 7,'IW')-1 start_date
FROM duaL
);

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