Oct 23 2023

MSSQL: Get size in KB of all database tables

SELECT
t.name AS TableName,
s.name AS SchemaName,
p.rows,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.object_id = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.name NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.object_id > 255
GROUP BY
t.name, s.name, p.rows
ORDER BY
TotalSpaceMB DESC, t.name

Source: https://stackoverflow.com/questions/7892334/get-size-of-all-tables-in-database


Oct 6 2023

EXCEL: Use VLOOKUP to Match Data from two Different Sheets

=VLOOKUP(A2;Sheet1!A:D;4;FALSE)

Find match of the A2 from Sheet1 in column A of Sheet2, return value of column D of Sheet2. Number 4 is index of column D in sheet2

=IF(ISERROR(VLOOKUP(A2; Sheet1!A:D; 4; FALSE)); "No Match"; D2 - VLOOKUP(A2; Sheet1!A:D; 4; FALSE))

if A2 is not in the sheet2 then “No Match”
if D2 from sheet1 = D from match in sheet2 then 0
if D2 from sheet1 <> D from match in sheet2 then display difference in values (X-Y)


May 4 2023

MSSQL: select only row with max value

Another approach for http://blog.interakt.hr/?p=610

select
	id, case_id, file_path, date_created
  from (
        select id, case_id, file_path, date_created, row_number() over (partition by case_id order by date_created desc) as rn
         from TMP_reminders
        ) as dtbl_unique_reminders
  where dtbl_unique_reminders.rn = 1

SOURCE: https://stackoverflow.com/questions/38912808/select-only-row-with-maxid-in-sql-server


Feb 25 2023

MSSQL – get file extension from file name

 select (case when FileName like '%.%' then reverse(left(reverse(FileName), charindex('.', reverse(FileName)) - 1)) else '' end) as File_Extension

source: https://stackoverflow.com/questions/16783026/t-sql-get-file-extension-name-from-a-column


Dec 7 2022

MSSQL: find string in string


SELECT * FROM tbl_companies WHERE CHARINDEX('test', Company_name) > 0 

Nov 13 2022

MSSQL – calculate season


SELECT
(case
		when month(GETDATE()) = 12  then case when day(GETDATE()) < 21 then 'autumn' else 'winter' end
		when month(GETDATE()) in ( 1,2 ) then 'winter'
		when month(GETDATE()) = 3 then case when day(GETDATE()) < 21 then 'winter' else 'spring' end
		when month(GETDATE()) in ( 4, 5 ) then 'spring'
		when month(GETDATE()) = 6  then case when day(GETDATE()) < 21 then 'spring' else 'summer' end
		when month(GETDATE()) in ( 7, 8 ) then 'summer'
		when month(GETDATE()) = 9  then case when day(GETDATE()) < 23 then 'summer' else 'autumn' end
		when month(GETDATE()) in ( 10, 11 ) then 'autumn'
end) as season

Feb 7 2022

MSSQL – Running total

ID/date_start/payment
1 2022-02-07 10:18:19.000 1.00
2 2022-02-07 10:18:19.000 2.00
3 2022-02-07 10:18:19.000 3.00
4 2022-02-07 10:18:19.000 4.00
5 2022-02-07 10:18:19.000 5.00
6 2022-02-06 10:18:19.000 6.00
7 2022-02-06 10:18:19.000 7.00
8 2022-02-06 10:18:19.000 8.00
9 2022-02-06 10:18:19.000 9.00
10 2022-02-05 10:18:19.000 10.00

SELECT a.date_start, a.payment,
    (SELECT SUM(payment) as payment
        FROM (SELECT x.date_start, SUM(x.payment) as payment FROM tmp_test as x GROUP BY x.date_start) b
        WHERE b.date_start <= a.date_start
) AS b
FROM (SELECT y.date_start,SUM(y.payment) as payment FROM tmp_test as y GROUP BY y.date_start) a

date_start/payment/b
2022-02-05 10:18:19.000 10.00 10.00
2022-02-06 10:18:19.000 30.00 40.00
2022-02-07 10:18:19.000 15.00 55.00

see test data @ https://stackoverflow.com/questions/860966/calculate-a-running-total-in-sql-server/10309947


Oct 15 2021

Excel: Find duplicates in column

Home > Conditional Formatting > Highlight Cells Rules > Duplicate Values


Sep 9 2021

MSSQL – table name as a variable


declare @schema varchar(50)
declare @table varchar(50)
declare @query nvarchar(500)

set @schema = 'dbo'
set @table = 'ACTY'

set @query = 'SELECT * FROM [DB_ONE].[' + @schema + '].[' + @table + '] '

EXEC sp_executesql @query

source: https://stackoverflow.com/questions/2838490/a-table-name-as-a-variable


Jun 14 2021

Excel – Remove Formulas From Worksheet But Keep The Values

  1. Copy
  2. Paste values

:)