In a PostgreSQL database, I've got a table payment with a column payment_date of type timestamp. My goal is to count payments made on Monday. The following query:
SELECT TO_CHAR(payment_date, 'day') FROM payment;
gives result such as:
thursday
friday
friday
monday
tuesday
However when I try to count Mondays like this:
SELECT COUNT(*) FROM payment
WHERE TO_CHAR(payment_date, 'day') = 'monday';
the result is 0 even though the previous query shows it should be greater than 0.
What's wrong about the second query?
Postgres docs says that "full lower case day name (blank-padded to 9 chars)"
May this select solve the problem? If there are trailing spaces, maybe your comparison fails.
SELECT COUNT(*)
FROM payment
WHERE RTRIM(TO_CHAR(payment_date, 'day')) = 'monday';
Related
I am relatively new to SQL and need to construct a small productivity query to retrieve results from the day prior (in this case, time of the first and last picking transaction, and then a count of total picks).
The issue is that obviously on Monday when this query runs, it returns no results and as such we never get the friday results. I would therefore like to know how to ignore the weekend days.
This is my query as of now:
SELECT EMPLOYEE_ID, MIN(END_TRAN_TIME), MAX(END_TRAN_TIME), COUNT(TRAN_TYPE)
FROM T_TRAN_LOG
WHERE WH_ID ='W376' AND TRUNC(END_TRAN_DATE) = TRUNC(sysdate-1) AND TRAN_TYPE IN ('301', '303', '305')
GROUP BY EMPLOYEE_ID;
Any help is greatly appreciated!
To ignore the weekend days, you can modify the query to check if the day of the week is not a weekend day (Saturday or Sunday).
SELECT EMPLOYEE_ID, MIN(END_TRAN_TIME), MAX(END_TRAN_TIME), COUNT(TRAN_TYPE)
FROM T_TRAN_LOG
WHERE WH_ID ='W376' AND
TRUNC(END_TRAN_DATE) = TRUNC(sysdate-1) AND
TRAN_TYPE IN ('301', '303', '305') AND
TO_CHAR(END_TRAN_DATE, 'DAY') NOT IN ('SATURDAY', 'SUNDAY')
GROUP BY EMPLOYEE_ID;
Hope this helps
A table with 7 rows having all weekdays as value. I just want a simple sql query to get all the values from the weekdays except weekends.
I hope this can help:
SELECT *
FROM [YourTableName] t
WHERE DATENAME(WEEKDAY,t.[YourDateField]) NOT IN ('Saturday','Sunday');
Simple, Try this :
SELECT *
FROM [YourTable]
WHERE DATENAME(WEEKDAY,[DateField]) <> 'Saturday'
AND DATENAME(WEEKDAY,[DateField]) <> 'Sunday'
There is a Similar question with solutions.
MySQL WEEKDAY() function returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday through to 6 for Sunday).
Syntax:
WEEKDAY(date) Where date is a date.
I am using Hive (which is similar to SQL, but the syntax can be little different for the SQL users). I have looked at the other stackoverflow, but they seems to be in the SQL with different syntax.
I am trying to the get the first day of the month through this query. This one gives me today's day. For example, if today is 2015-04-30, then result would be 2015-04-01. Thanks!
select
cust_id,
FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') as first_day_of_month_transaction
--DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as first_day_of_month_transaction --SQL format. Not compatible in Hive.
from
customers;
Try this
date_format(current_date,'yyyy-MM-01')
To get the first day of the month, you can use:
date_add(<date>,
1 - day(<date>) )
Applied to your expression:
date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
1 - day(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
)
But this will work for any column in the right format.
SELECT TRUNC(rpt.statement_date,'MM') will give you the first day of month.
You can use this query to calculate the First day of the current month
Select date_add(last_day(add_months(current_date, -1)),1);
Instead of current_date, you can use the date field name.
last_day => Gives the last day of current month
add_months with -1 => Gives previous month
date_add with 1 => Add one day
Another way - select concat(substring(current_date,1,7),'-01');
I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1
This should be doable, but how can I extract the day of the week from a field containing data in date format with Netezza SQL? I can write the following query:
SELECT date_part('day',a.report_dt) as report_dt
FROM table as a
but that gives me the day of the month.
thanks for any help
The below queries give day numbers for any week,month,year for a particular date.
--Day of Week
SELECT EXTRACT(dow FROM report_dt) FROM table;
--Day of Month
SELECT DATE_PART('day', report_dt) FROM table;
--Day of Year
SELECT EXTRACT(doy FROM report_dt) FROM table;
Netezza is just ANSI SQL, originally derived from PostgreSQL. I'd expect this to work.
select extract(dow from a.report_dt) as report_dt
from table as a
Returns values should range from 0 to 6; 0 is Sunday. You might expect that to be an integer, but in PostgreSQL at least, the returned value is a double-precision floating point.
If you want to extract directly the day name :
Select to_char(date, 'Day') as Day_Name From table;
In Netezza SQL, SELECT EXTRACT(dow FROM report_dt) would return values 1 to 7. 1 is Sunday, 7 is Saturday.