ItemsBought procedure SQL code example

Example: ItemsBought procedure SQL

DROP TABLE IF EXISTS items;
CREATE TABLE items (
    id int NOT NULL PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    dateBought DATE NOT NULL
);

INSERT INTO items (id, name, dateBought) VALUES (0, 'Eggs', '2020-08-01');
INSERT INTO items (id, name, dateBought) VALUES (1, 'Couch', '2020-07-23');
INSERT INTO items (id, name, dateBought) VALUES (2, 'Potatoes', '2020-01-11');
INSERT INTO items (id, name, dateBought) VALUES (3, 'Llama', '2020-09-01');
/*
Finish the ItemsBought procedure so that it returns only items whose dateBought 
is between @date and one month after @date (both inclusive). 
For each item it should return:*/

GO
Alter PROCEDURE ItemsBought (@Date date) AS
BEGIN
 
 
  DECLARE @Date2 date
  SET @Date2 =  Dateadd(Month, 1, @Date)
 
  SELECT  Day(dateBought) day, Month(dateBought) Month, name  FROM items
	where dateBought between @date and @date2
	
END;
GO

EXEC ItemsBought '2020-08-01'

Tags:

Sql Example