top of page

SQL String-Zusammenbau für Datum und Uhrzeit / Build Sql string for date and time

(alle Versionen/all versions)

Dienstag, 6. Juni 2023

Hintergrund / Background

DateTimeFromParts

Result

Odbc format as text constant

Result



Hintergrund / Background


In der Praxis muss man gelegentlich Datums- und Uhrzeitwerte von einem Format in ein anderes übernehmen, für SQL-Strings oder für Auswertungen zusammenbauen etc.

Beispiel: Wenn man sich in komplexen Anwendungsfällen ad-hoc-SQL zusammenbauen muss und nicht unbedingt den Aufwand mit Parametern betreiben möchte, dann es gibt diverse Möglichkeiten, um Datumswerte von einer parametrierten Variable oder einem Datenfeldinhalt in eine SQL-Anweisung zu überführen.

Faustregel: In dynamisch zusammengebauten SQL-Texten muss man nicht immer mit Parametern arbeiten, solange dabei keine SQL-Injection-Gefahr besteht!

⚠️Wenn SQL-Injection-Gefahr besteht, müssen unbedingt Parameter auch in dynamisch ausgeführten SQL-Anweisungen eingesetzt werden!

Die Verwendung von Parametern und auch die Art des dynamisch zusammengebauten SQL-Ausdrucks kann auch Performance-Unterschiede bedeuten!⚠️

Wenn man den Wert für Datum und Uhrzeit z.B. als feste Konstante übergeben kann, dann sollte dies bevorzugt werden (siehe nachfolgende zweite Variante).


In practice, you occasionally have to transfer date and time values ​​from one format to another, assemble them for SQL strings or for data evaluations, etc.

Example: If you have to assemble ad hoc SQL in complex use cases and you do not necessarily want to implement the dynamic SQL execution with parameters, then there are various ways of converting date values ​​from a parameterized variable or a data field content into a dynamic SQL statement.

Rule of thumb: You don't always have to work with parameters in dynamically assembled SQL texts as long as there is no risk of SQL injection! ⚠️If there is a risk of SQL injection, parameters must be used in dynamically executed SQL statements!

The use of parameters and the type of dynamically assembled SQL expression can also mean performance differences!⚠️

If you can pass the value for date and time as a fixed constant, for example, then this should be preferred (see the second variant below).


DateTimeFromParts

-- TODO Your date/time value.
-- This could also be a value from a table field.
DECLARE @dtImportTimeStamp DATETIME

SET @dtImportTimeStamp = GETDATE()

-- We now build a SQL string text for the date and time, which can be used in a dynamically assembled SQL statement  
-- This uses the SQL function "DATETIMEFROMPARTS" to build the SQL text
DECLARE @sImportTs NVARCHAR(100)

SET @sImportTs = N'DATETIMEFROMPARTS(year, month, day, hour, minute, seconds, 0)'

SET @sImportTs = REPLACE(@sImportTs, N'year', CAST(DATEPART(YEAR, @dtImportTimeStamp) as nvarchar(4)))

SET @sImportTs = REPLACE(@sImportTs, N'month', CAST(DATEPART(MONTH, @dtImportTimeStamp) as nvarchar(2)))

SET @sImportTs = REPLACE(@sImportTs, N'day', CAST(DATEPART(DAY, @dtImportTimeStamp) as nvarchar(2)))

SET @sImportTs = REPLACE(@sImportTs, N'hour', CAST(DATEPART(HOUR, @dtImportTimeStamp) as nvarchar(2)))

SET @sImportTs = REPLACE(@sImportTs, N'minute', CAST(DATEPART(MINUTE, @dtImportTimeStamp) as nvarchar(2)))

SET @sImportTs = REPLACE(@sImportTs, N'seconds', CAST(DATEPART(SECOND, @dtImportTimeStamp) as nvarchar(2)))

SELECT @sImportTs

Result

DATETIMEFROMPARTS(2023, 6, 3, 0, 1, 49, 0)

Odbc format as text constant

-- TODO Your date/time value.
-- This could also be a value from a table field.
DECLARE @dtImportTimeStamp DATETIME

SET @dtImportTimeStamp = GETDATE() 
  
-- We now build a SQL string text for the date and time, which can be used in a dynamically assembled SQL statement  
-- In this case we will build a Odbc SQL string constant text, which usually leads to better performance, depending on the scenario
DECLARE @sImportTs NVARCHAR(100)

SET @sImportTs = N'{ ts ''year-month-day hour:minute:seconds''}'

SET @sImportTs = REPLACE(@sImportTs, N'year', FORMAT(DATEPART(YEAR, @dtImportTimeStamp), '0000'))

SET @sImportTs = REPLACE(@sImportTs, N'month', FORMAT(DATEPART(MONTH, @dtImportTimeStamp), '00'))

SET @sImportTs = REPLACE(@sImportTs, N'day', FORMAT(DATEPART(DAY, @dtImportTimeStamp), '00'))

SET @sImportTs = REPLACE(@sImportTs, N'hour', FORMAT(DATEPART(HOUR, @dtImportTimeStamp), '00'))

SET @sImportTs = REPLACE(@sImportTs, N'minute', FORMAT(DATEPART(MINUTE, @dtImportTimeStamp), '00'))

SET @sImportTs = REPLACE(@sImportTs, N'seconds', FORMAT(DATEPART(SECOND, @dtImportTimeStamp), '00'))

SELECT @sImportTs

Result

{ ts '2023-06-03 01:49:13'}

Note, a date-only result format would need to be something like this in Odbc format:

{ d '2023-06-03'}


bottom of page