Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. CREATE FUNCTION GET_TZTIME
  2. (@DT AS DATETIME,
  3. @TZ AS VARCHAR(12))
  4. RETURNS DATETIME
  5. AS
  6. BEGIN
  7. -- DECLARE VARIABLES
  8. DECLARE @NEWDT AS DATETIME
  9. DECLARE @OFFSETHR AS INT
  10. DECLARE @OFFSETMI AS INT
  11. DECLARE @DSTOFFSETHR AS INT
  12. DECLARE @DSTOFFSETMI AS INT
  13. DECLARE @DSTDT AS VARCHAR(10)
  14. DECLARE @DSTEFFDT AS VARCHAR(10)
  15. DECLARE @DSTENDDT AS VARCHAR(10)
  16.  
  17. -- GET THE DST parameter from the provided datetime
  18. -- This gets the month of the datetime provided (2 char value)
  19. SELECT @DSTDT = CASE LEN(DATEPART(month, @DT))
  20. WHEN 1
  21. then '0' + CONVERT(VARCHAR(2),DATEPART(month, @DT))
  22. ELSE CONVERT(VARCHAR(2),DATEPART(month, @DT)) END
  23. -- This gets the occurrence of the day of the week within the month
  24. --(i.e. first Sunday, or second Sunday...) (1 char value)
  25. SELECT @DSTDT = @DSTDT + CONVERT(VARCHAR(1),(DATEPART(day,@DT) + 6) / 7)
  26. -- This gets the day of the week for the provided datetime (1 char value)
  27. SELECT @DSTDT = @DSTDT + CONVERT(VARCHAR(1),DATEPART(dw, @DT))
  28. -- This gets the hour for the provided datetime (2 char value)
  29. SELECT @DSTDT = @DSTDT + CASE LEN(DATEPART(hh, @DT))
  30. WHEN 1
  31. then '0' + CONVERT(VARCHAR(2),DATEPART(hh, @DT))
  32. ELSE CONVERT(VARCHAR(2),DATEPART(hh, @DT)) END
  33. -- This gets the minutes for the provided datetime (2 char value)
  34. SELECT @DSTDT = @DSTDT + CASE LEN(DATEPART(mi, @DT))
  35. WHEN 1
  36. THEN '0' + CONVERT(VARCHAR(2),DATEPART(mi, @DT))
  37. ELSE CONVERT(VARCHAR(2),DATEPART(mi, @DT)) END
  38.  
  39. -- This query gets the timezone information from the TIME_ZONES table
  40. -- for the provided timezone
  41. SELECT
  42. @OFFSETHR=offset_hr,
  43. @OFFSETMI=offset_mi,
  44. @DSTOFFSETHR=dst_offset_hr,
  45. @DSTOFFSETMI=dst_offset_mi,
  46. @DSTEFFDT=dst_eff_dt,
  47. @DSTENDDT=dst_END_dt
  48. FROM time_zones
  49. WHERE timezone_cd = @TZ AND
  50. @DT BETWEEN eff_dt AND end_dt
  51.  
  52. -- Checks to see if the DST parameter for the datetime provided
  53. -- is within the DST parameter for the timezone
  54. IF @DSTDT BETWEEN @DSTEFFDT AND @DSTENDDT
  55. BEGIN
  56. -- Increase the datetime by the hours and minutes assigned to the timezone
  57. SET @NEWDT = DATEADD(hh,@DSTOFFSETHR,@DT)
  58. SET @NEWDT = DATEADD(mi,@DSTOFFSETMI,@NEWDT)
  59. END
  60. -- If the DST parameter for the provided date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement