CREATE FUNCTION [dbo].[SystemDateTimeF] (@ADateStr CHAR(8)) RETURNS DATETIME AS
/*
  Created by Kent Johansen, September 2024
  Get system date and time
*/
BEGIN
  DECLARE @TimeZone VARCHAR(100)
  DECLARE @CurrentDateStr CHAR(8)

  SELECT @TimeZone = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'TimeZone'
  IF ISNULL(@TimeZone, '') = ''
    SET @TimeZone = 'Romance Standard Time'

  IF ISNULL(@ADateStr, '') <> ''
    SET @CurrentDateStr = @ADateStr
  ELSE
    SELECT @CurrentDateStr = SystemDate FROM System WITH (NOLOCK) WHERE Number = 1

  RETURN CONVERT(DATETIME, @CurrentDateStr + ' ' + CONVERT(VARCHAR(8), CURRENT_TIMESTAMP AT TIME ZONE 'Romance Standard Time' AT TIME ZONE @TimeZone, 108))
END