SQL Server snippets

How to find numeric columns in a temporary table

Example 1

Starting from the #People temporary table

CREATE TABLE #People(
	[Name] [nvarchar](50) NOT NULL,
	[Surname] [nvarchar](50) NOT NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[Age] INT NULL,
	[Weight] [decimal](5,2)
) ON [PRIMARY];
INSERT INTO #People
  ( [Name], [Surname], [StartDate], [EndDate], [Age], [Weight])
VALUES
  ('Mary', 'Green', '2021-07-01 00:00:00.000', '2021-07-01 14:00:00.000',35,87.4),
  ('Mary', 'Green', '2021-07-01 14:00:00.000', '2021-07-02 08:30:00.000',35,87.4),
  ('Mary', 'Green', '2021-07-03 00:00:00.000', '2021-07-03 08:00:00.000',35,87.4),
  ('Andrew', 'Green', '2021-07-02 08:30:00.000', '2021-07-03 08:45:00.000',40,62.8),
  ('Andrew', 'Green', '2021-07-03 08:45:00.000', '2021-07-03 09:00:00.000',40,62.8),
  ('Paul', 'Brown', '2021-07-04 06:00:00.000', '2021-07-04 09:00:00.000',18,82.1);

Table #Times

you can find the numeric columns of the table excluding others data_type, with the following query

SELECT
  select COLUMN_NAME from tempdb.INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME LIKE '#People%'
and DATA_TYPE NOT IN ('VARCHAR','CHAR','NVARCHAR','DATETIME','TIMESTAMP') 
Table #Times aggregation with details

You can find an interactive version of this example following this link .

Back to SQL Server cookbook page