Free cookie consent management tool by TermsFeed Policy Generator
  • SQL Server Video
  •    20 page views
  •   1137 video views
  • 2024, November 14, Thursday

#028 How to Get Dates from All Tables in SQL Server

Aprenderemos nesse vídeo como obter dinamicamente as datas mínimas e máximas de todas as tabelas existentes no seu banco de dados SQL Server.

We'll get to know the techniques:

1. List all existing columns of the tables (INFORMATION_SCHEMA. COLUMNS)

:
  • To list all columns from all tables in the database:

    SELECT TABLE_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA. COLUMNS
    

2. Filter columns by data type (WHERE, DATA_TYPE, IN)

:
  • To list columns of a specific data type:

    SELECT TABLE_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA. COLUMNS
    WHERE DATA_TYPE IN ('int', 'varchar')
    

3. Get

minimum and maximum column values (MIN, MAX):
  • To find the minimum and maximum values of a column:

    SELECT MIN(Column), MAX(Column)
    FROM Table
    

4. Convert SQL result to XML format (FOR XML, RAW, AUTO)

:
  • To convert results of a query to XML:

    SELECT Column1, Column2
    FROM Table
    FOR XML RAW, ELEMENTS
    

5. Declare Text Type Variable (DECLARE,

VARCHAR):
  • To declare a text type variable:

    DECLARE @MinhaVariavel VARCHAR(50)
    

6. Concatenate Table Values to Variable (@VARIAVEL)

:
  • To Concatenate Values from a Column to a Variable:

    SELECT @MinhaVariavel = @MinhaVariavel + Column
    FROM Table
    

7. Create

Dynamic SQL Query:
  • To create a Dynamic SQL query:

    DECLARE @ConsultaDinamica NVARCHAR(MAX)
    SET @ConsultaDinamica = 'SELECT Column FROM Table'
    

8. Ensure that the text-type variable is in the valid format for the

object (QUOTENAME, IDENTIFIER):
  • To ensure that a variable is a valid object name:

    SET @MinhaVariavel = QUOTENAME(@MinhaVariavel)
    

9. Get all

characters except the last character (RIGHT, LEN) from the text type
  • variable:To get all characters except the last one from a text variable:

    SET @MinhaVariavel = RIGHT(@MinhaVariavel, LEN(@MinhaVariavel) - 1)
    

10. Manipulate XML structure data (NODES, VALUE)

:
  • To manipulate data in an XML:

    SELECT structure
      XmlColumn.value('(Path/To/Node)[1]', 'varchar(50)') AS Value
    FROM Table
    

11. Automatically create table with query result (INTO, FROM)

:
  • To create a table based on the result of a query:

    SELECT Column1, Column2
    INTO NewTable
    FROM ExistingTable
    

12. Run Dynamic SQL Query (

EXEC):
  • To run a Dynamic SQL query:

    EXEC sp_executesql @ConsultaDinamica
    

Remember to tailor the examples to the specific structure of your database and the needs of your query.

This content contains
  • Content Video
  • Language Portuguese
  • Duration 12m 2s
  • Subtitles Não

  • Reading time 1 min 36 seg

avatar
Fabio Santos

Data Scientist and Consultant for Digital and Analytics Solutions


  • Share

Youtube Channel

@fabioms

Subscribe now