Knowee
Questions
Features
Study Tools

Write a PL/pgSQL block to find out count of completed month between two dates enter by the user.

Question

Write a PL/pgSQL block to find out count of completed month between two dates enter by the user.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a PL/pgSQL block that calculates the count of completed months between two dates entered by the user:

DO $$
DECLARE
    start_date DATE;
    end_date DATE;
    month_count INT;
BEGIN
    -- Replace '2022-01-01' and '2022-12-31' with the dates entered by the user
    start_date := '2022-01-01';
    end_date := '2022-12-31';

    month_count := (EXTRACT(YEAR FROM end_date) - EXTRACT(YEAR FROM start_date)) * 12 + (EXTRACT(MONTH FROM end_date) - EXTRACT(MONTH FROM start_date));

    -- If the end date is earlier in the month than the start date, subtract one month
    IF EXTRACT(DAY FROM end_date) < EXTRACT(DAY FROM start_date) THEN
        month_count := month_count - 1;
    END IF;

    RAISE NOTICE 'The count of completed months is %', month_count;
END $$;

This block first calculates the difference in years and months separately, then combines them to get the total difference in months. If the day of the end date is earlier in the month than the day of the start date, it subtracts one month from the total, because that month is not completed. Finally, it outputs the result using RAISE NOTICE.

Please replace '2022-01-01' and '2022-12-31' with the dates entered by the user.

This problem has been solved

Similar Questions

Write a PL/SQL anonymous block which will perform date and time operations. Declare a variable ‘d’ of type DATETIME and initialize with the value '25-Jan-2020'. And then perform below date and time  operations and print the result:1. Print the date after adding 4 months to the date d2. Print only the year part present in the date d3. Print the last day of the month based on the date d4. Assume that the current date is ‘2020-10-25’. Display the number of month between the current date and the date d

Write a query to display the month and sum of fine_amount collected on the month between July to October. Give an alias name as 'monthly_fine_amount' also display the records in ascending order based on month.

Write a PL/SQL simple procedure named display_dates with 2 input parameter. The first input parameter is d1 of type date. The second input parameter is d2 of type date. This procedure will display the dates between d1 and d2(exclusive), each in a newline.Use the below skeleton:Procedure name: display_datesInput parameter: d1 of type date, d2 of type dateNote:Do not change the procedure nameDo not change the argument count and orderDo not change the output text.Instructions:1. Create the procedure successfully2. Once the procedure is created, check the functionality of the procedure using different anonymous block call.3. DO NOT submit the anonymous block. Submit only the CREATE PROCEDURE query.

Write a query to display the month and sum of fine_amount collected on the month between July to October. Give an alias name as 'monthly_fine_amount' also display the records in ascending order based on month.Note:1. Display only the month and sum of fine_amount collected in the month between July to October from bill table. (month will be 01, 02, 03,...12)2. Specify the alias name without any mistake.3. Order by is mandatory4. Group by month to get the month wise sum of fine_amount5. Compare your executed result with the sample output given below.

Write a query to display the count of bills which has completed payment without fine on the month of October 2017. Give an alias name as 'bills_paid_withoutfine'.Note:1. Display only the count of bills which has completed payment without fine on the month of October 2017 from bill table.2. Specify the alias name without any mistake.3. Specify condition on payment_date and fine_amount. payment_date must be NOT NULL and it must be October 2017 and fine_amount must be NULL.4. Compare your executed result with the sample output given below.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.