0

Is it possible to query column ranges for a SQL Server table?

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = '{table}'
Dale K
  • 25,246
  • 15
  • 42
  • 71
susanna
  • 1,395
  • 3
  • 20
  • 32
  • Does this answer your question? [How can I get column names from a table in SQL Server?](https://stackoverflow.com/questions/1054984/how-can-i-get-column-names-from-a-table-in-sql-server) – Mukyuu Jan 10 '20 at 04:40
  • what do you mean by `column ranges` ? – Squirrel Jan 10 '20 at 05:54
  • You need to explain what you mean by column ranges and provide some sample output of what you expect to return from the query. – Martin Cairney Jan 13 '20 at 03:43

1 Answers1

1

Use dynamic SQL to apply a column name filter:

DECLARE @DynamicQuery AS NVARCHAR(MAX)
    SET @DynamicQuery = 'SELECT '+STUFF((SELECT  ',' +name AS [text()] FROM sys.columns WHERE object_id=object_id('TABLE')

    -- Add column range filter here.
         AND name LIKE 'N%'  
    -- Or, if column names are numeric, cast and compare range
         -- AND cast(name as int) BETWEEN 10 AND 20 

    ORDER BY  name FOR XML PATH('')),  1, 1, '')+' FROM TABLE';
    EXEC(@DynamicQuery)
Shep
  • 638
  • 3
  • 15