SB - Date OperationsWrite 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 dNote:Do not change the value of the date d.Display the result in the same order. Do not change the order of operations.Sample output:SubmitPreviously Executed QueriesSaveExecutePrevious Submission1SET SERVEROUTPUT ON;2DECLARE 3 d DATE := '25-Jan-2020';4BEGIN 5 --Write your code here6END;7
Question
SB - Date OperationsWrite 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 dNote:Do not change the value of the date d.Display the result in the same order. Do not change the order of operations.Sample output:SubmitPreviously Executed QueriesSaveExecutePrevious Submission1SET SERVEROUTPUT ON;2DECLARE 3 d DATE := '25-Jan-2020';4BEGIN 5 --Write your code here6END;7
Solution
Here is the PL/SQL anonymous block that performs the required date and time operations:
SET SERVEROUTPUT ON;
DECLARE
d DATE := TO_DATE('25-Jan-2020', 'DD-Mon-YYYY');
current_date DATE := TO_DATE('2020-10-25', 'YYYY-MM-DD');
BEGIN
-- 1. Print the date after adding 4 months to the date d
DBMS_OUTPUT.PUT_LINE('Date after adding 4 months: ' || TO_CHAR(ADD_MONTHS(d, 4), 'DD-Mon-YYYY'));
-- 2. Print only the year part present in the date d
DBMS_OUTPUT.PUT_LINE('Year part of the date: ' || TO_CHAR(d, 'YYYY'));
-- 3. Print the last day of the month based on the date d
DBMS_OUTPUT.PUT_LINE('Last day of the month: ' || TO_CHAR(LAST_DAY(d), 'DD-Mon-YYYY'));
-- 4. Display the number of month between the current date and the date d
DBMS_OUTPUT.PUT_LINE('Number of months between current date and d: ' || MONTHS_BETWEEN(current_date, d));
END;
/
This block declares a date variable 'd' and initializes it with '25-Jan-2020'. It then performs the following operations:
- Adds 4 months to 'd' and prints the result.
- Extracts and prints the year part from 'd'.
- Finds and prints the last day of the month based on 'd'.
- Calculates and prints the number of months between the current date '2020-10-25' and 'd'.
Similar Questions
Write a JAVA program in which take date(DDMMYYYY) from user and displaynext day date(DDMMYYYY) as output.Example:Input: date=09, month=-06, year=1992Output: date=10, month=-06, year=1992Note:-1. Consider condition for leap year2. Consider number of days in month of February based on leap year ( if leapyear then February days =29, else days = 28 ) )3. Consider number of days either 30 or 31 based on month entered by user
Write a PL/pgSQL block to find out count of completed month between two dates enter by the user.
Write a C program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31st December 2010 when the user enters 12 31 2010, say. The program has to work out when superscripts “th”, “nd”, “st”, and “rd” need to be appended to the day value. The programmer should not forget 1st, 2nd, 3rd, 4th; and then 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Testcases:Input:02 11 2021Output:11th February 202
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.
Which SQL query retrieves all records with dates falling within the current month?
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.