Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use this stored procedure: It takes the year as input parameter. It returns columns (with their tables name) that have values under the inputted year.</p> <pre><code>ALTER PROCEDURE [dbo].[sp_GetAllDateColumns] @startDateYear [INT] AS BEGIN SET nocount ON; -- --------------- DECLARE @table_name NVARCHAR(100) DECLARE @col_name VARCHAR(100) DECLARE @sqlCommand NVARCHAR(1000) DECLARE @count INT -- --------------- DECLARE @ResultArray TABLE ( table_name NVARCHAR(100), col_name VARCHAR(100) ) -- ---------------- DECLARE table_cursor CURSOR FOR SELECT ( SCH.name + '.' + TAB.name )AS tablename, COL.name AS colname FROM sys.schemas SCH, sys.tables TAB, sys.columns COL, sys.types TYP WHERE COL.system_type_id = TYP.system_type_id AND TAB.object_id = COL.object_id AND SCH.schema_id = TAB.schema_id AND TYP.name IN ( 'datetime', 'date' ); OPEN table_cursor FETCH next FROM table_cursor INTO @table_name, @col_name WHILE @@FETCH_STATUS = 0 BEGIN SET @sqlCommand = 'SELECT @cnt = COUNT(*) FROM ' + @table_name + ' WHERE datepart(year,' + + @col_name + ' ) &lt; ' + CONVERT(NVARCHAR, @startDateYear) EXECUTE Sp_executesql @sqlCommand, N'@cnt int OUTPUT', @cnt=@count output IF @count &gt;= 1 BEGIN INSERT INTO @ResultArray(table_name, col_name)VALUES (@table_name,@col_name) END FETCH next FROM table_cursor INTO @table_name, @col_name END CLOSE table_cursor DEALLOCATE table_cursor --Return result SELECT * FROM @ResultArray END </code></pre> <p>To call this procedure from SQL Server, all you have to do is to execute this query:</p> <pre><code>EXEC [dbo].[sp_GetAllDateColumns] @startDateYear = 2003 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload