Announcement

Collapse
No announcement yet.

Problem beim Abfragen einer Spalte mit Zeitangaben

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem beim Abfragen einer Spalte mit Zeitangaben

    Hallo zusammen

    Bei unserem ERP-System wird in einem Feld die genaue Uhrzeit der Buchung angegeben. Nun wird die Zeit auf der SQL-Datenbank in einem anderen Format angegeben. Nun sollte ich aus der Datenbank eine Abfrage erstellen in der genau dieser Zeitwert eine Rolle spielt. Doch ich bringe es einfach nicht fertig die Werte korrekt zu knvertieren.
    2 Beispiele:

    Wert angezeigt im ERP-System = 07:44:54, Wert auf der Danenbank = 28206
    Wert angezeigt im ERP-System = 13:41:58, Wert auf der Danenbank = 49806

    der Datentyp auf der Datenbank ist Integer.

    Weiss jemand von Euch wie ich den Integer Datentyp richtig konvertieren kann damit das korrekte Zeitformat auf der Auswertung angezeigt wird?

    Ach ja, ich benutze MS-SQL2005 und das ERP is MS-Axapta 3.0

    Wäre echt froh um Hilfe. Vielen Dank schon im Voraus
    Gruss Marcel

  • #2
    Sekunden ab 00:00 Uhr

    Hallo,

    so wie es sich darstellt, wird die Zeit in Sekunden ab 00:00 Uhr in der DB gespeichert.
    (Machen viele ERP Systeme so.)

    Den Wert einfach in Stunden , Minuten , Sekunden aufteilen.


    Wobei:

    07:44:54 = 27894
    13:41:58 = 49318

    ergeben muesste


    TimeOfDay declaration = timeOfDay Variable { , Variable } ;

    variable = identifier [ option ]

    option = Arrayoptions | initialization

    //Declaration of a time variable, time1
    timeOfDay time1;

    //Declaration and initialization of a time variable to 00:21:35
    timeOfDay time2 = 1295;

    Time Literals

    timeOfDay variables can be used as literals in the same way as integers are used as literals.
    Automatic Conversions

    The same automatic conversions that are available for integers are also available for timeOfDay variables (Booleans, enums, and reals).
    Using Times in Expressions

    Any operations that can be performed with integers can also be performed with timeOfDay variables.
    Overview of timeOfDay

    Keyword


    timeOfDay

    Size


    32 bits

    Scope of data type


    [0; 86400]

    Default value


    0

    Implicit conversions


    Automatically converted to real, boolean, and enum

    Explicit conversions


    str2time, time2str


    mfg

    Bernd
    Zuletzt editiert von bdittmar; 30.05.2011, 19:04. Reason: Add aus X++

    Comment


    • #3
      Originally posted by bdittmar View Post
      Hallo,
      ...
      Den Wert einfach in Stunden , Minuten , Sekunden aufteilen.

      ...
      m.a.W. in TSQl z. B. so[HIGHLIGHT="SQL"]IF OBJECT_ID (N'dbo.IntToTimeStr') IS NOT NULL
      DROP FUNCTION dbo.IntToTimeStr
      GO

      CREATE FUNCTION dbo.IntToTimeStr
      (@Time int)
      RETURNS TABLE AS RETURN
      (
      SELECT CASE WHEN Hour < 10
      THEN '0' + CAST (Hour AS CHAR(1))
      ELSE CAST (Hour AS CHAR(2))
      END + ':'
      + CASE WHEN Minute < 10
      THEN '0' + CAST (Minute AS CHAR(1))
      ELSE CAST (Minute AS CHAR(2))
      END + ':'
      + CASE WHEN Second < 10
      THEN '0' + CAST (Second AS CHAR(1))
      ELSE CAST (Second AS CHAR(2))
      END AS TimeStr
      FROM (
      SELECT @Time / 3600 AS Hour
      , (@Time - ((@Time / 3600) * 3600)) / 60 AS Minute
      , @Time - (@Time / 3600 * 3600) - (((@Time - ((@Time / 3600) * 3600)) / 60) * 60) AS Second
      ) AS Times
      )[/HIGHLIGHT][HIGHLIGHT="SQL"]
      WITH Times AS
      ( SELECT 27894 TimeInt UNION ALL
      SELEcT 49318
      )
      SELECT *
      FROM Times
      CROSS APPLY dbo.IntToTimeStr (Times.TimeInt) AS Time[/HIGHLIGHT]
      [HIGHLIGHT="Result"]TimeInt TimeStr
      ----------- --------
      27894 07:44:54
      49318 13:41:58[/HIGHLIGHT]

      Comment


      • #4
        Hallo marcel66,
        hier eine etwas kürzere Funktion

        [HIGHLIGHT="SQL Funktion erstellen - "]
        IF OBJECT_ID (N'dbo.IntToTimeStr') IS NOT NULL
        DROP FUNCTION dbo.IntToTimeStr
        GO

        CREATE FUNCTION dbo.IntToTimeStr(@Time int)
        RETURNS varchar(8)
        BEGIN
        DECLARE @strZeit varchar(8);
        SET @strZeit = RIGHT('0'+CAST( @Time/(60*60) AS VARCHAR),2) + ':' --Stunden
        + RIGHT('0'+CAST((@Time/60)%60 AS VARCHAR),2) + ':' --Minuten
        + RIGHT('0'+CAST((@Time%60) AS VARCHAR),2); --Sekunden
        RETURN (@strZeit);
        END
        [/HIGHLIGHT]

        [HIGHLIGHT="Funktion verwenden - "]
        SELECT 27894 AS TimeInt ,dbo.IntToTimeStr(27894) AS TimeStr
        UNION
        SELECT 49318 AS TimeInt ,dbo.IntToTimeStr(49318) AS TimeStr
        [/HIGHLIGHT]

        [HIGHLIGHT="Ergebnis - "]TimeInt TimeStr
        ----------- --------
        27894 07:44:54
        49318 13:41:58
        [/HIGHLIGHT]
        Zuletzt editiert von knoxyz; 10.06.2011, 12:27.
        Und Falk Prüfer sprach: Formatierung von SQL in Beiträgen

        Comment

        Working...
        X