syntax error when cast in EXTRACT - sql

I just try this and I got a syntax error with tdate (it a DATE type)
select *
from treatment
where EXTRACT(month FROM cast (date as TIMESTAMP) tdate) = EXTRACT(month FROM cast (date as TIMESTAMP) current_date)
is anyone can help me out..??
thanks!!!!

I do not think you need to bother casting either field since you should be able to extract the month directly from both. I would try:
select *
from treatment
where extract(month from tdate) = extract(month from current_date)

Is this what you're looking for instead:
select *
from treatment
where EXTRACT(month FROM cast (tdate as TIMESTAMP)) =
EXTRACT(month FROM cast (current_date as TIMESTAMP))
It should be cast(field as type) where you have cast(date as type).

Related

I would like to extract the month and day from a date

My code
SELECT
PARSE_DATE('%Y%m%d', CAST(date AS STRING)) AS date,
EXTRACT(DAY from date) AS day_of_month,
EXTRACT(MONTH from date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
I would like to turn the date's in my column which is in the form 20170101 into 2017-01-01.
Then I would like to extract the month and day from this. However I keep getting an error:
No matching signature for function EXTRACT for argument types: DATE_TIME_PART FROM INT64. Supported signatures: EXTRACT(DATE_TIME_PART FROM DATE); EXTRACT(DATE_TIME_PART FROM TIMESTAMP [AT TIME ZONE STRING]); EXTRACT(DATE_TIME_PART FROM DATETIME); EXTRACT(DATE_TIME_PART FROM TIME) at [1:60]
Below few options for BigQuery Standard SQL - to avoid multiple pre-parsing
Option 1
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_month,
EXTRACT(WEEK FROM date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
FROM `project.dataset.table`,
UNNEST([PARSE_DATE('%Y%m%d', CAST(date AS STRING))]) date
Option 2
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_month,
EXTRACT(WEEK FROM date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
FROM (
SELECT * REPLACE(PARSE_DATE('%Y%m%d', CAST(date AS STRING)) AS date)
FROM `project.dataset.table`
)
You need to re-parse the date for each extract():
SELECT PARSE_DATE('%Y%m%d', CAST(date AS STRING)) as date,
EXTRACT(DAY from PARSE_DATE('%Y%m%d',
EXTRACT(MONTH from PARSE_DATE('%Y%m%d', CAST(date AS STRING))) as week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
However, if the date is already a number in YYYYMMDD format, why not just use arithmetic functions:
select mod(date, 100) as day,
mod(floor(date / 100), 100) as month

Fetch yesterday details using extract in SQL

consultation(patient_id, cdate, doctor_id)
I want the details of patients which was treated yesterday using extract
But I am getting error
ORA-00911: invalid character
My code:
select *
from consultation
where extract(day from cdate) = extract(day from sysdate) - '1';
You may avoid extract and simply use:
select *
from consultation
where trunc(cdate) = trunc(sysdate-1)
Here I use trunc to remove the time part; also, notice that by using extract to compare the day, you will get not only records of yesterday, but even records from the past months.
If you need to get all the records where the day is 6 (assuming that sysdate is May, 7), no matter the month or year, you can use:
where extract(day from cDate) = extract(day from sysdate -1)
Prefer not to use EXTRACT on the date column, it is less efficient, especially if there's an index or partition on the date column. simply use TRUNC on SYSDATE
select * from consultation where
cdate >= TRUNC(sysdate) - 1
AND cdate < TRUNC(SYSDATE)
you need to put integer 1 not string '1'
select * from consultation where
extract(day from cdate) = extract(day from sysdate) - 1;

unable to typecast timestamp to date in Group By

I am unable to typecast timestamp to date type in the Group By of my SQL Select statement.
SELECT geography_id,
listed_at::DATE,
EXTRACT(YEAR FROM listed_at) AS year,
EXTRACT(MONTH FROM listed_at) AS month,
EXTRACT(day FROM listed_at) AS day,
Count(*) AS active_listing_count,
SUM(list_price) AS sum_of_listing_price,
Date_part('day', current_date :: timestamp - listed_at :: timestamp) AS days_on_market,
COUNT(num_bathrooms) AS total_bathrooms,
COUNT(num_bedrooms) AS total_bedrooms
FROM properties
WHERE expired_at IS NULL
GROUP BY geography_id,
listed_at::DATE
ORDER BY listed_at::DATE DESC;
I am getting this error:
ERROR: column "properties.listed_at" must appear in the GROUP BY clause or be used in an aggregate function
Each occurrence of listed_at in select list should be casted to date:
SELECT geography_id,
listed_at::DATE,
EXTRACT(YEAR FROM listed_at::date) AS year,
EXTRACT(MONTH FROM listed_at::date) AS month,
EXTRACT(day FROM listed_at::date) AS day,
count(*) AS active_listing_count,
SUM(list_price) AS sum_of_listing_price,
date_part('day', current_date::timestamp - listed_at::date) AS days_on_market,
COUNT(num_bathrooms) AS total_bathrooms,
COUNT(num_bedrooms) AS total_bedrooms
FROM properties
WHERE expired_at IS NULL
GROUP BY geography_id,
listed_at::DATE
ORDER BY listed_at::DATE DESC;

postgresql case when date and sorting by date

Im trying to filter results by a specific date but when I use the below code I only get results where CloseDate matches current date. It doesnt include the results from ResolvedDate when CloseDate is Null.
I tried with :
(CASE WHEN (Closedate IS Null) then ResolvedDate, ELSE CloseDate END) AS FinalDate
but then it states:
"column FinalDate does not exist"
Any other way I can do this?
Here is the code thus far. Appreciate your help.
SELECT
id,
(CASE WHEN (Closedate IS Null) then ResolvedDate, ELSE CloseDate END),
FROM cases
WHERE (EXTRACT (month FROM Closedate) = EXTRACT(month FROM current_date))
AND ( EXTRACT(day from Closedate) = EXTRACT(day FROM current_date))
Assuming you want to match the year as well:
SELECT id, COALESCE(Closedate, ResolvedDate) AS cdate
FROM cases
WHERE date_trunc('day', COALESCE(Closedate, ResolvedDate))
= date_trunc('day', now())
Per documentation: COALESCE, date_trunc()
If you want to ignore the year:
WHERE to_char(COALESCE(Closedate, ResolvedDate), 'MMDD')
= to_char(now(), 'MMDD')
A bit more on that:
How do you do date math that ignores the year?
You should use COALESCE function http://www.postgresql.org/docs/current/static/functions-conditional.html
Try this:
SELECT
id,
COALESCE(CloseDate, ResolvedDate) AS FinalDate
FROM
cases
WHERE
(EXTRACT (month FROM COALESCE(CloseDate, ResolvedDate)) = EXTRACT(month FROM current_date)) AND
(EXTRACT (day from COALESCE(CloseDate, ResolvedDate)) = EXTRACT(day FROM current_date))
That should do it...

How to get only date/month part from a date in postgres SQL

Hello All
I have a table in my pg admin database.There is an employee table in this table.Having the field:-
1)name
2)date_of_birth
Now the scenario is that I want to know the birth day for current date and upcoming 20 days
For example if current date is 28-Jan-2013 then
1)from_date=28-Jan-2013
2)to_date=16-feb-2013
I want to select all the records from the table for which the
date_of_birth
lies between 28-Jan and 16-feb
Try this:
SELECT *
FROM bdaytable
WHERE bdate >= '2013-01-28'::DATE
AND bdate <= '2013-02-16'::DATE;
You may also try overlaps:
SELECT *
FROM bdaytable
WHERE (bdate, bdate)
OVERLAPS ('2013-01-28'::DATE, '2013-02-16'::DATE);
with extract, month, day:
SELECT *
FROM bdaytable
WHERE Extract(month from bdate) >= Extract(month from '2013-01-28'::DATE)
AND Extract(month from bdate) <= Extract(month from '2013-02-16'::DATE)
AND Extract(day from bdate) >= Extract(day from '2013-01-28'::DATE)
AND Extract(day from bdate) <= Extract(day from '2013-02-16'::DATE);
Incorporating Now() and interval to make the query dynamic with current date:
SELECT *
FROM bdaytable
WHERE Extract(month from bdate) >= Extract(month from Now())
AND Extract(month from bdate) <= Extract(month from Now() + Interval '20 day')
AND Extract(day from bdate) >= Extract(day from Now())
AND Extract(day from bdate) <= Extract(day from Now() + Interval '20 day');
select *
from employee
where
to_char(date_of_birth, 'MMDD') between
to_char(current_date, 'MMDD') and to_char(current_date + 20, 'MMDD')
I think this should work. Use interval.
SELECT *
FROM Employee
WHERE Date_Of_Birth >= now() AND Date_Of_Birth <= now() + interval '20 day'
If you want to get any birth date between these days (and year doesn't matter), then that would be slightly different.
EDIT
If year doesn't matter, while I'm sure there is a better way, this could work. Basically it's just converting all years to a common year (in this case 2000) -- you could do the same with your input parameter as well.
(Date_Of_Birth + (2000-EXTRACT(YEAR FROM Date_Of_Birth) || ' years')::interval)
Curious what others have used in the past as this is probably not the most efficient.
Good luck.