Tuesday 20 September 2016

Select data from date range between two dates

We have table named Product_Sales and it holds data like this
Product_ID | Sold_by | Qty | From_date  | To_date
-----------+---------+-----+------------+-----------
3          | 12      | 7   | 2013-01-05 | 2013-01-07
6          | 22      | 14  | 2013-01-06 | 2013-01-10
8          | 11      | 9   | 2013-02-05 | 2013-02-11
Now what is the query if I want to select sales data between two dates from a date range?
For example, I want to select sales data from 2013-01-03 to 2013-01-09.
Answer is:
SELECT  *
FROM    Product_sales
WHERE   From_date BETWEEN '2013-01-03' AND '2013-01-09'
        OR To_date BETWEEN '2013-01-03' AND '2013-01-09'
        OR From_date <= '2013-01-03'
        AND To_date >= '2013-01-09'
Hint: If one of From_date or To_date is between the dates, or From_date is less than start date and To_date is greater than the end date.
Courtesy: FallenAngel (Stack OverFlow Ref.)

Monday 19 September 2016

Delete user from SQL Server

SELECT  s.name
FROM    sys.schemas s
WHERE   s.principal_id = USER_ID('rds');
GO

ALTER AUTHORIZATION ON SCHEMA::db_accessadmin TO dbo;
GO

DROP USER rds;
GO

Popular Posts

Stock Overflow - Feeds