--this query finds the current ( as of the date the query is run) age of the person.
declare @dob datetime
set @dob = '07/12/1966'
select CASE
WHEN dateadd(year, datediff (year, @dob, GETDATE()),@dob) > GETDATE()
THEN datediff (year, @dob, GETDATE()) - 1
ELSE datediff (year, @dob, GETDATE())
END as Age
--select dateadd(year, datediff (year, @dob, GETDATE()),@dob)
--select datediff (year, @dob, GETDATE())
This blog is created to add all the information and experiences that I have learnt while working on MS sql server as a DBA. The blog is also updated frequently with the real world problem that I encounter hands on at work and the resolutions to resolve them. I hope this blog will be of some use to you and you will revisit. Thank you for stopping by and you are welcome to leave comments.
Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts
Monday, July 18, 2016
Thursday, October 16, 2014
Stored procedure with an OUTPUT parameter
CREATE procName1 (@intClientId int, @account varchar(50), @intCount int OUTPUT)
AS
SELECT @intCount = COUNT (*) FROM Clients where clientId = @intClientId
SELECT @intCount
GO
--procedure call
Declare @out int
EXECUTE procName1 999, 'ABCSDRF',@out OUTPUT
SELECT @out
AS
SELECT @intCount = COUNT (*) FROM Clients where clientId = @intClientId
SELECT @intCount
GO
--procedure call
Declare @out int
EXECUTE procName1 999, 'ABCSDRF',@out OUTPUT
SELECT @out
Wednesday, March 28, 2012
Check the backup process
Script to see how the backup is progressing.
SELECT percent_complete , (estimated_completion_time/1000)/60 Estimated_completion_time_Mins , (total_elapsed_time/1000)/60 Total_Elapsed_Time_Mins ,DB_NAME(Database_id) DBName ,* FROM
sys.dm_exec_requests WHERE session_id = (the spid for running the back up - you can find by running sp_who2 active)
SELECT percent_complete , (estimated_completion_time/1000)/60 Estimated_completion_time_Mins , (total_elapsed_time/1000)/60 Total_Elapsed_Time_Mins ,DB_NAME(Database_id) DBName ,* FROM
sys.dm_exec_requests WHERE session_id = (the spid for running the back up - you can find by running sp_who2 active)
Friday, January 13, 2012
Restore database
use master
GO
Alter Database INACTIVECLIENTS2009SET SINGLE_USER With ROLLBACK IMMEDIATE
GO RESTORE DATABASE INACTIVECLIENTS2009FROM DISK = 'E:\DatabaseArchivesNotRestored\INACTIVECLIENTS2009_20110112_1053am.bak'WITH REPLACEGO
Alter Database INACTIVECLIENTS2009SET MULTI_USER With ROLLBACK IMMEDIATEGO USE INACTIVECLIENTS2009;GO
EXEC sp_updatestats
I was getting error "database cannot be restored because it is in use" this was resolved when I set the database to single_user mode
Alter Database INACTIVECLIENTS2009SET SINGLE_USER With ROLLBACK IMMEDIATE
Then after successfully restoring it I changed it to the multi user mode.
GO
GO
Alter Database INACTIVECLIENTS2009SET SINGLE_USER With ROLLBACK IMMEDIATE
GO RESTORE DATABASE INACTIVECLIENTS2009FROM DISK = 'E:\DatabaseArchivesNotRestored\INACTIVECLIENTS2009_20110112_1053am.bak'WITH REPLACEGO
Alter Database INACTIVECLIENTS2009SET MULTI_USER With ROLLBACK IMMEDIATEGO USE INACTIVECLIENTS2009;GO
EXEC sp_updatestats
I was getting error "database cannot be restored because it is in use" this was resolved when I set the database to single_user mode
Alter Database INACTIVECLIENTS2009SET SINGLE_USER With ROLLBACK IMMEDIATE
Then after successfully restoring it I changed it to the multi user mode.
GO
Thursday, October 27, 2011
Replacing multiple occurences of space with one space in a string
Given string : THIS IS A BAD STRING
Out put string : THIS IS A BAD STRING
The Logic
-----------------
@str AS VARCHAR(1000) = 'THIS IS A BAD STRING',@Token AS VARCHAR(1) = '~'
SELECT REPLACE(REPLACE(REPLACE(@str,' ','~ '),' ~',''),'~',' ')
Out put string : THIS IS A BAD STRING
The Logic
-----------------
- In the given string replcae the space with a token (e,g. ~ in this case) and a space e.g. '~ '
- Repace the space and token in the string result from step 1 with an empty string.' ~'
- Replace the result of the above atring with a token and the space
@str AS VARCHAR(1000) = 'THIS IS A BAD STRING',@Token AS VARCHAR(1) = '~'
SELECT REPLACE(REPLACE(REPLACE(@str,' ','~ '),' ~',''),'~',' ')
Counting the occurence of a substring within a string
Today I learnt a special techinque from SQL Server magazine to find the total number of substrings within a string. The logic is so simple that I am surprised why I never used it.
E.g. abcdballmnopballqrstball
We need to find the total number of occurences of the substring "ball" in the given string.
Logic
--------------
Find the substring ball and replace it with '' e.g.abcdmnopqrst
Find the length of the new string 12
Subtract the new length from the lenth of the original string 24-12 = 12
Divide the new number with the length of the substring 12/4 = 3
Here is the script - taken form the November issue of SQL server magazine
DECLARE
@str AS VARCHAR(1000) = ' abcdballmnopballqrstball ';
@substr AS VARCHAR(1000) = 'ball';
SELECT LEN(@str) - LEN(REPLACE(@str,(@substr,''>/LEN(@substr)
This is so awesome!
E.g. abcdballmnopballqrstball
We need to find the total number of occurences of the substring "ball" in the given string.
Logic
--------------
Find the substring ball and replace it with '' e.g.abcdmnopqrst
Find the length of the new string 12
Subtract the new length from the lenth of the original string 24-12 = 12
Divide the new number with the length of the substring 12/4 = 3
Here is the script - taken form the November issue of SQL server magazine
DECLARE
@str AS VARCHAR(1000) = ' abcdballmnopballqrstball ';
@substr AS VARCHAR(1000) = 'ball';
SELECT LEN(@str) - LEN(REPLACE(@str,(@substr,''>/LEN(@substr)
This is so awesome!
Friday, October 14, 2011
COALESCE - Selecting each record with all not nul values
I had three rows of data that had the same acount number but differnt variation in the three columns. I had to select each account with not null column . This was achieved by the following...
My data
create table #tmpcol(c219 int, c220 int,c221 int)
INSERT INTO #tmpcol
SELECT null,220,null
INSERT INTO #tmpcol
SELECT 219,null,null
INSERT INTO #tmpcol
SELECT null,null,221
select top 1 * from #tmpcolorderby coalesce(100*c219,10*c220,c221) desc
My data
create table #tmpcol(c219 int, c220 int,c221 int)
INSERT INTO #tmpcol
SELECT null,220,null
INSERT INTO #tmpcol
SELECT 219,null,null
INSERT INTO #tmpcol
SELECT null,null,221
select top 1 * from #tmpcolorderby coalesce(100*c219,10*c220,c221) desc
Subscribe to:
Posts (Atom)