Sunday, 18 August 2013

concatenate EVERY 3rd CHARACTER using FUNCTION and PROCEDURE

concatenate EVERY 3rd CHARACTER using FUNCTION and PROCEDURE----------------

---------------------------USING PROCEDURE---------------------------------
CREATE PROC name_store(@stringname VARCHAR(300))
AS
BEGIN
DECLARE @char_concat varchar(50)=''
DECLARE @i int=3
WHILE(@i<LEN(@stringname))
BEGIN
select @char_concat=@char_concat+substring(@stringname,@i,1)
SET @i=@i+3
END
PRINT @char_concat
END
EXEC name_store 'JFSOAIRFGASLKEHLRUWAH...UDSFJGNZDSFH'

--------------------USING FUNCTION------------------------------------------------

ALTER FUNCTION get_concat_char(@FULLNAME VARCHAR(50))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @I INT=3
DECLARE @NAME VARCHAR(100)=''
WHILE(@I<LEN(@FULLNAME))
BEGIN
SET @NAME=@NAME+SUBSTRING(@FULLNAME,@I,1)
SET @I=@I+3
END
RETURN @NAME;
END
SELECT  DBO.get_concat_char('JZSDFJJBVSUDFSMAZBXFCVH') 

Print DATABASES and NO OF TABLES in each DATABASE--using CURSOR

------------CREATE PROC for Print DATABASES and NO OF TABLES in each DATABASE---------

ALTER PROC concatination
AS
BEGIN
DECLARE @qry varchar(100)
DECLARE @NAME VARCHAR(MAX)=''
DECLARE c1 CURSOR FOR SELECT name FROM sys.databases
OPEN C1
FETCH NEXT FROM C1 INTO @NAME
WHILE(@@FETCH_STATUS<>-1)
BEGIN
set @qry= 'select '+char(39)+@name+char(39)+',count(*) from '+@name+'.sys.tables'
--EXEC(@qry)
fetch next from c1 into @name
PRINT @qry
END
CLOSE C1
DEALLOCATE C1
END
EXEC concatination

EMP, DEPT Sample script

/****** Object:  Table [dbo].[DEPT]    Script Date: 19-05-2016 06:58:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET A...