goosegle

ToDateTime

Feb 26th, 2021
703
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- =======================================================================
  2. -- Author:      Goose
  3. -- Create date: 2021-02-26
  4. -- Description: Convierte una cadena con formato 'yyyyMMddhhmmss' en fecha
  5. -- =======================================================================
  6. CREATE FUNCTION [dbo].[ToDateTime]
  7. (
  8.     @valor VARCHAR(14)
  9. )
  10. RETURNS DateTime
  11. AS
  12. BEGIN
  13.     DECLARE @Result DateTime
  14.     DECLARE @year int, @month int, @day int, @hour int, @minute int, @second int, @valorEntero numeric
  15.  
  16.     SELECT @valorEntero = CAST(@valor as numeric)
  17.     SELECT @year = FLOOR(@valorEntero / 10000000000)
  18.     SELECT @valorEntero -= @year * 10000000000
  19.     SELECT @month = FLOOR(@valorEntero / 100000000)
  20.     SELECT @valorEntero -= @month * 100000000
  21.     SELECT @day = FLOOR(@valorEntero / 1000000)
  22.     SELECT @valorEntero -= @day * 1000000
  23.     SELECT @hour = FLOOR(@valorEntero / 10000)
  24.     SELECT @valorEntero -= @hour * 10000
  25.     SELECT @minute = FLOOR(@valorEntero / 100)
  26.     SELECT @valorEntero -= @minute * 100
  27.     SELECT @second = @valorEntero
  28.  
  29.     SELECT @Result = DATETIMEFROMPARTS(@year, @month, @day, @hour, @minute, @second, 0)
  30.  
  31.     RETURN @Result
  32. END
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×