Created
October 23, 2013 23:37
-
-
Save josuebasurto/7128753 to your computer and use it in GitHub Desktop.
Funcion de SQL que permite convertir una fecha (historica) en tiempo transcurrido
Hace 1 segundo/minuto/hora/dia/mes/año
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE FUNCTION GetLastActiveDateTime | |
| ( | |
| @lastActiveDate Datetime | |
| ) | |
| RETURNS varchar(30) | |
| AS | |
| BEGIN | |
| DECLARE @LastActivity varchar(100) | |
| SET @LastActivity = ''; | |
| DECLARE @today datetime, @nowLastActiveDate datetime | |
| DECLARE @years int, @months int, @days int, @hours int, | |
| @minutes int, @seconds int, @h int, @m int, @s int | |
| SELECT @today = GETDATE() | |
| SELECT @nowLastActiveDate = DATEADD(year, | |
| DATEDIFF(year, @lastActiveDate, @today), @lastActiveDate) | |
| SELECT @years = DATEDIFF(year, @lastActiveDate, @today) - | |
| (CASE WHEN @nowLastActiveDate > @today THEN 1 ELSE 0 END) | |
| SELECT @months = MONTH(@today - @nowLastActiveDate) - 1 | |
| SELECT @days = DAY(@today - @nowLastActiveDate) - 1 | |
| SELECT @h = DATEDIFF(HOUR, @lastActiveDate, @today) | |
| SELECT @m = DATEDIFF(MINUTE, @lastActiveDate, @today) | |
| SELECT @s = DATEDIFF(SECOND, @lastActiveDate, @today) | |
| SET @hours = (@h%24) | |
| SET @minutes = (@m%60) | |
| SET @seconds = (@s%60) | |
| SET @LastActivity = | |
| (CASE | |
| WHEN @years = 1 THEN ' 1 año | |
| (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @years > 1 THEN convert(varchar(3),@years) + | |
| ' años (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @months > 1 THEN ' 1 mes (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @months = 1 THEN convert(varchar(3),@months) + | |
| ' meses (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @days = 1 THEN ' 1 dia (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @days > 1 THEN convert(varchar(3),@days) + | |
| ' días (' + convert(varchar, @lastActiveDate, 105) + ')' | |
| WHEN @hours = 1 THEN ' 1 hora ' | |
| WHEN @hours > 1 THEN convert(varchar(3),@hours) + ' horas ' | |
| WHEN @minutes = 1 THEN ' 1 minuto ' | |
| WHEN @minutes > 1 THEN convert(varchar(3),@minutes) + ' minutos ' | |
| WHEN @seconds = 1 THEN ' 1 segundo ' | |
| WHEN @seconds > 1 THEN convert(varchar(3),@seconds) + ' segundos' | |
| ELSE convert(varchar, @lastActiveDate, 105) | |
| END) | |
| RETURN 'Hace ' + @LastActivity; | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment