- Get link
- X
- Other Apps
Posts
Showing posts from March, 2021
Converting Columns into rows with their respective data in ms sql server
- Get link
- X
- Other Apps
declare @T table (ScripName varchar(50), ScripCode varchar(50), Price int) insert into @T values ('20 MICRONS', '533022', 39) select 'ScripName' as ColName, ScripName as ColValue from @T union all select 'ScripCode' as ColName, ScripCode as ColValue from @T union all select 'Price' as ColName, cast(Price as varchar(50)) as ColValue from @T
UNPIVOT in sql server
- Get link
- X
- Other Apps
SELECT id ,entityId ,indicatorname ,indicatorvalue FROM (VALUES (1, 1, 'Value of Indicator 1 for entity 1', 'Value of Indicator 2 for entity 1', 'Value of Indicator 3 for entity 1'), (2, 1, 'Value of Indicator 1 for entity 2', 'Value of Indicator 2 for entity 2', 'Value of Indicator 3 for entity 2'), (3, 1, 'Value of Indicator 1 for entity 3', 'Value of Indicator 2 for entity 3', 'Value of Indicator 3 for entity 3'), (4, 2, 'Value of Indicator 1 for entity 4', 'Value of Indicator 2 for entity 4', 'Value of Indicator 3 for entity 4') ) AS Category(ID, EntityId, Indicator1, Indicator2, Indicator3) UNPIVOT ( indicatorvalue FOR indicatorname IN (Indicator1, Indicator2, Indicator3) ) UNPIV;
unpivot in sql server above version 2016
- Get link
- X
- Other Apps
Declare @YourTable Table ([ID] varchar(50),[Col1] varchar(50),[Col2] varchar(50)) Insert Into @YourTable Values (1,'A','B') ,(2,'R','C') ,(3,'X','D') Select A.[ID] ,Item = B.[Key] ,Value = B.[Value] From @YourTable A Cross Apply ( Select * From OpenJson((Select A.* For JSON Path,Without_Array_Wrapper )) Where [Key] not in ('ID','Other','Columns','ToExclude') ) B
get table listed above n number of rows
- Get link
- X
- Other Apps
DECLARE @TableName varchar(max)=NULL SELECT @TableName=COALESCE(@TableName+',','')+t.TABLE_CATALOG+'.'+ t.TABLE_SCHEMA+'.'+o.Name FROM sysindexes AS i INNER JOIN sysobjects AS o ON i.id = o.id INNER JOIN INFORMATION_SCHEMA.TABLES T ON T.TABLE_NAME=o.name WHERE i.indid < 2 --AND OBJECTPROPERTY(o.id,'IsMSShipped') = 0 AND i.rowcnt >5 --AND o.xtype !='TF' ORDER BY o.name ASC print @tablename