Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Some SQL Tips

4.53/5 (14 votes)
11 Jul 2016CPOL 7.3K  
Some SQL Server Query tricks

1. I am back after a very long break. So, I somehow forgot SQL Server ‘SA’ Password, and this is how I recovered that.

  • Stop SQL SERVER
  • Change the startup parameters by adding add -m;
  • Start SQL Server
  • Open command prompt and type SQLCMD

Method 1: Create new user and add to systemadmin role

SQL
CREATE LOGIN recovery1 WITH PASSWORD = 'abc123%';
sp_addsrvrolemember 'recovery1', 'sysadmin';
GO;

Now login using created user and change the ‘Sa’ password.

Method 2: Change Password

SQL
EXEC sp_password NULL, abc123!@#$', 'sa';
GO;

Method 3: Give system admin roles to the Windows user

SQL
EXEC sp_addsrvrolemember 'Softcat\tharaka_r', 'sysadmin';
GO;

2. I could not remember who wrote this query, but I am sure it was not me. :p So, I just simplified that.

SQL
DECLARE @RunDate AS DATETIME = getdate()
SELECT CONCAT (DATEPART(yyyy,DATEADD(dd,30,@RunDate)),'-', _
DATEPART(mm,DATEADD(dd,30,@RunDate)),'-', _
DATEPART(dd,DATEADD(dd,30,@RunDate))) AS DateNow , 30 AS Value;

The simplified version is as given below:

SQL
DECLARE @RunDate AS DATETIME = getdate();
SELECT CONVERT(VARCHAR(10),DATEADD (dd,30, @RunDate),120) AS DateNow , 30 AS Value;

Filed under: CodeProject, SQL
Tagged: Database, SQL, SQL Server

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)