Search code examples
sqlsql-serverproceduresql-date-functions

SQL Server : date Interval output


How I can get T-SQL to show me all info betwen for example 2001 and 2010? It's a procedure:

CREATE PROCEDURE proc_neue_mitarbeiter_004
    @Personalnummer varchar(10),
    @Name varchar(30),
    @Vorname varchar(30),
    @Geburtsdatum date,
    @Telefon varchar(30),
    @Mobil varchar(30),
    @Email varchar(50),
    @Raum varchar(10),
    @Ist_Leiter char(1),  
    @AbtBezeichnung varchar(30), -- hier wird dann kein Abteilungs Nummer geschrieben, sonst ein Namen von Abteilung die unter Nummer in die Tabelle "Abteilung" steht
    @steuerklasse tinyint
AS
BEGIN
    DECLARE @Abteilung_ID AS INT
    SET @Abteilung_ID = (SELECT id 
                         FROM Abteilung 
                         WHERE Bezeichnung = @AbtBezeichnung) -- lokale Variable (interne)

    INSERT INTO Mitarbeiter(Personalnummer, Name, Vorname, Geburtsdatum, 
                            Telefon, Mobil, Email, Raum, Ist_Leiter, 
                            Abteilung_ID, steuerklasse) 
    VALUES (@Personalnummer, @Name, @Vorname, @Geburtsdatum,
            @Telefon, @Mobil, @Email, @Raum, @Ist_Leiter,
            @Abteilung_ID, @steuerklasse) -- lokale Variable als ausgangspunkt

    -- Man kann auch mit subselect (select spalte from table) aus anderen tabellen die werte nehmen
END
GO

EXEC proc_neue_mitarbeiter_004 '200001', 'Stark', 'Tony', '01.01.2001',
                               null, null, null, null, 'Y',
                               'Vertrieb', '1' 

Solution

  • Your stored procedure performs an INSERT operation. You're asking for a SELECT operation.

    It's also not entirely what field you want to use to extract data between 2001 and 2010. However, your query most likely would look something like this:

    SELECT 
        * 
    FROM 
        Mitarbeiter 
    WHERE 
        Geburtsdatum BETWEEN '2001-01-01' AND '2010-12-31'. 
    

    You might have to adjust the date formatting to fit your regional settings, and the column to pull data from the field you actually need (I'm having a hard time imagining you're employing 7-year olds).