SQL data base in Restoring mode after successfully restored

        Most of the people facing an issue stuck database in restoring mode, after successful restore of  the database. Why this is happening ? This is likely caused by  WITH No RECOVERY, missing of adding with recovery option will causing the issue. 

This is the situation database is looking for latest transaction log after restoring.




        To resolve this issue restore the database again is a time consuming thing, So we can force the database out of restoring mode by executing below script.

RESTORE DATABASE YourDbName WITH RECOVERY

ALTER DATABASE YourDbName SET ONLINE


How to get next business day after excluding weekends in SQL

    Below query is helpful to find next business date by excluding weekends. We can use  same logic to find out the weekend.

1. Find next business day excluding weekend holiday.

   DECLARE @Current_date DATETIME = '14 Nov 2020'

   SELECT CASE

   WHEN (((DATEPART(DW,  @Current_date) - 1 ) + @@DATEFIRST ) % 7) IN (6)

           THEN @Current_date + 2

   WHEN (((DATEPART(DW,  @Current_date) - 1 ) + @@DATEFIRST ) % 7) IN (5)

           THEN @Current_date + 3

   ELSE @Current_date + 1 END AS next_business_date

2. Check today is business day or not in SQL server.

    SELECT (CASE

        WHEN (((DATEPART(DW,  GETDATE()) - 1 ) + @@DATEFIRST ) % 7) IN (0,6)

        THEN 0

        ELSE 1

      END) AS is_business_day


Different query method in SQL Server

 

    Here we are discussing about simple query technique used in sql server . Please go through the below example.


1. Use below query to crated temp Data set in SQL.

CREATE TABLE #TempTable (UserName VARCHAR(20),Age INT,Gender VARCHAR(10),Amount MONEY)

INSERT INTO #TempTable

SELECT 'Clark',27,'Male',500

UNION ALL

SELECT 'Jack_dani',29,'Male',100

UNION ALL

SELECT 'Libsa',18,'Female',1000

UNION ALL

SELECT 'Libsa',18,'Female',500

UNION ALL

SELECT 'Jp-dani',30,'Male',100


2. Query to select data contain underscore in User Name.

SELECT  * FROM #TempTable WHERE CHARINDEX('_',UserName) > 0

3. Query to filter column gender is Male.

SELECT  * FROM #TempTable WHERE Gender = 'Male'

4. Query to find sum of total amount grouping age column.

SELECT Age,SUM(Amount) As Total

FROM #TempTable

GROUP BY Age


5. Query to find the users whose age is greater than 27. 

SELECT  * FROM #TempTable WHERE age > 27