[Solved] T-SQL (Un)Pivot Table

I usually use dynamic sql. Something like this create table #T ( ID int, aText1 varchar(4), aText2 varchar(4), aInt1 int, aInt2 int ) insert into #T select 1, ‘ABC1’, ‘XYZ1’, 2, 20 union select 2, ‘ABC1’, ‘XYZ2’, 3, 25 union select 3, ‘ABC2’, ‘XYZ2’, 1, 30 union select 4, ‘ABC2’, ‘XYZ1’, 4, 35 declare @sql … Read more

[Solved] How to extract a string between two of the SAME delimiters T-SQL?

Use CHAR_INDEX twice: SELECT *, SUBSTRING(path, pos1 + 1, pos2 – pos1 – 1) FROM tests CROSS APPLY (SELECT NULLIF(CHARINDEX(‘\’, path), 0)) AS ca1(pos1) CROSS APPLY (SELECT NULLIF(CHARINDEX(‘\’, path, pos1 + 1), 0)) AS ca2(pos2) — NULLIF is used to convert 0 value (character not found) to NULL Test on db<>fiddle 1 solved How to … Read more

[Solved] How to convert varchar time to 24hours Time in SQL Server [closed]

Not clear whether you want your result as a VARCHAR or an actual TIME type, so here’s both: DECLARE @Table TABLE ( TXN_TIME VARCHAR(6) ) INSERT INTO @Table SELECT ‘053124’ UNION SELECT ‘173932’ UNION SELECT ‘011815’ UNION SELECT ‘120349’ UNION SELECT ‘134207’ SELECT TXN_TIME, LEFT(TXN_TIME,2) + ‘:’ + SUBSTRING(TXN_TIME,3,2) + ‘:’ + SUBSTRING(TXN_TIME,5,2) + ‘:000000’ … Read more

[Solved] Need Query – Row count with loop

declare @timediff table(id int identity(1,1),Name varchar(50),logtime datetime) insert into @timediff Select Name,logtime from timediff declare @datetimeDiff int declare @i int=1 while(@i<=(Select count(*) from @timediff)) Begin Select @datetimeDiff=datediff(hour,logtime, (Select logtime from @timediff where id=@i+1)) from @timediff where id=@i if(@datetimeDiff>=1) BEGIN Select @datetimeDiff –You can write your update code here END Set @i=@i+2 END 0 solved Need … Read more

[Solved] sql update (help me )

First, figure out which records need to be updated: select * from tbl_order o inner join tbl_group g on g.grp_id = o.grp_id inner join tbl_indicator i on i.grp_nbr = g.grp_nbr and i.sect_nbr = g.sect_nbr where g.indicat != i.indicat Now, modify the query to update those records with the correct grp_id. Notice that I’ve added an … Read more

[Solved] T-SQL INSERT INTO disabling Constraints check

Constraint is on the table not a single statement Kind of ugly but Put it in a transaction and take a tablock begin transaction ALTER TABLE branchOffice NOCHECK CONSTRAINT ALL insert into branchOffice with (tablock) — Re-enable the constraints on a table ALTER TABLE branchOffice WITH CHECK CHECK CONSTRAINT ALL commit transation; 3 solved T-SQL … Read more

[Solved] SQL query with order by clause

‘Transaction’ is a pair of take + return. It’s identity is computed from source data so OPERATORs could be grouped the way you need. The query may fail on data with unpaired OPERATORs. declare @tbl table ( OPERATOR int, PRODUCT varchar(50), [USER NAME] varchar(100), [TIME STAMP] datetime); insert into @tbl(OPERATOR, PRODUCT, [USER NAME], [TIME STAMP]) … Read more

[Solved] How can I select 4 distinct values from 2 tables containing many rows of data in SQL Server?

Something like this? SELECT TOP 4 g.GalleryID ,g.GalleryTitle ,g.GalleryDate ,MAX(m.MediaThumb) AS MaxMediaThumb FROM Galleries g INNER JOIN Media m ON g.GalleryID = m.GalleryID GROUP BY g.GalleryID, g.GalleryTitle, g.GalleryDate 3 solved How can I select 4 distinct values from 2 tables containing many rows of data in SQL Server?

[Solved] What’s wrong with this SQL?

SELECT COALESCE(s.state, c.state) AS state , COALESCE(s.city, c.city) AS city , COALESCE(s.Suppliers, 0) AS Suppliers , COALESCE(c.Consumers, 0) AS Consumers FROM ( SELECT Tb_Supplier.State , Tb_Supplier.City , COUNT(Tb_Supplier.Name) AS Suppliers FROM Tb_Supplier GROUP BY Tb_Supplier.City , Tb_Supplier.State ) AS s FULL OUTER JOIN ( SELECT Tb_Consumer.State , Tb_Consumer.City , COUNT(Tb_Consumer.Name) AS Consumers FROM Tb_Consumer GROUP … Read more