Crear una función definida por el usuario

11/09/2006 - 17:29 por David Ortiz | Informe spam
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor que dos),
quiero crear la siguiente función, pero me devuelve una serie de errores. He
revisado la ayuda y también otros ejemplos de creación de funciones y no doy
en que me estoy quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) + @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.

Preguntas similare

Leer las respuestas

#11 jcac
12/09/2006 - 17:46 | Informe spam
Creala la funcion en otra bd que tengas la compatibilidad en 80 y desde tu
bd en 70 la llamas y te funcionará, de esta manera

Select [bd con compatibilidad al 80].dbo.calculatiempo('20060109',
Getdate())

Saludos

:D

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:%
Hola, ya descubrí cual es el problema.
Se debe a que tenemos la compatibilidad de la base de datos en 70 (SQL
Server 7.0), porque es necesario para que funcione un sistema hecho con un
generador de código (el cual se aloca si se lo cambiammos). Por lo menos
ya sé por donde abordarlo.

Gracias por el tiempo prestado.

Saludos,
David Ortiz.

"jcac" wrote in message
news:%
Hola David, asi como indicas debe de ser un problema de tu sql server,
lei un anterior post tuyo e indicas que estas en SQL Server 2000 (te iba
a decir que verifiques tu version con un select @@version), entonces no
deberías de tener problemas.

Por lo menos a mi nunca me ha pasado lo que indicas, quizás te falte
algún service pack, no lo se que mas pueda ser.

Bueno quizás a alguien le haya pasado.

Lamento no poderte ayudar

Saludos

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola jcac, gracias por tu molestia.
Resultado : Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 103
A RETURN statement with a return value cannot be used
in this context.

Como podrás ver, el problema no es la conversión de tipos de datos, sino
que es la sintaxis misma, además que pide que declare los parametros de
la función y no se acepta el Return, al parecer uds. si la han podido
crear.
Como dije anteriormente, he revisado en la ayuda, ejemplos, etc. y
debería estar correcta su definición, pero el SQL no la quiere, otra
cosa que he visto, es que cuando le doy al asistente "Crear nueva
función" me genera el script y sin tocarle nada, al darle "chequear la
sintaxis", devuelve el error que "está mal declarado 'CREATE FUNCTION',
ó sea el mismo que me devuelve en la primera línea. Comienzo a creer que
es un problema del SQL Server.

Saludos.


"jcac" wrote in message
news:Ol4$
Hola

Me copie tu funcion esto es lo que tengo espero te sirva

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter FUNCTION dbo.CalculaTiempo(@FechaInicial AS datetime,@FechaFinal
AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Minute) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Hour) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Day) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Month) + ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Year) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

Saludos




"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
¿?

Precisamente, esta función la quiero crear en SQL Server 2000.

Saludos.

"DNC" wrote in message
news:
Recuerda que las funciones fueron introducidas en mssql 2000.


Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any
kind. The entire risk
usage and all it's consequences including data loss and hardware
damage are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se
guarda; solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente,
responder discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola Noldis, gracias por contestar.
Me sigue dando exactamente los mismos errores que al inicio. ¿Qué
otra cosa puede ser?
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 23
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 30
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 37
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 44
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 47
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 52
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 59
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 88
A RETURN statement with a return value cannot be used in this
context.

Saludos!

"Noldis Chumacero" wrote in message
news:%23$
David,

Tienes problemas de conversión, aqui te envío la versión que me
funciona con los cambios

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial)
+ @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial)
+ @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial)
+ @Year

IF @Minute > 0
BEGIN
SET @Resultado = CAST(@Minute AS VARCHAR(10)) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = CAST(@Hour AS VARCHAR(10)) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = CAST(@Day AS VARCHAR(10)) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = CAST(@Month AS VARCHAR(10))+ ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = CAST(@Year AS VARCHAR(10)) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

SELECT [test_db].[dbo].[CalculaTiempo]('1/2/2006', GETDATE())


Saludos
Ing. Noldis Chumacero Ch.
Dpto. Sistemas, AeroSur.
Santa Cruz de la Sierra - Bolivia

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor
que dos), quiero crear la siguiente función, pero me devuelve una
serie de errores. He revisado la ayuda y también otros ejemplos de
creación de funciones y no doy en que me estoy quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) -
DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial)
+ @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) + @Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) -
DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.

































Respuesta Responder a este mensaje
#12 David Ortiz
12/09/2006 - 17:57 | Informe spam
Correcto!, funciona!

Un inmenso 'gracias'

Saludos.

"jcac" wrote in message
news:
Creala la funcion en otra bd que tengas la compatibilidad en 80 y desde tu
bd en 70 la llamas y te funcionará, de esta manera

Select [bd con compatibilidad al 80].dbo.calculatiempo('20060109',
Getdate())

Saludos

:D

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:%
Hola, ya descubrí cual es el problema.
Se debe a que tenemos la compatibilidad de la base de datos en 70 (SQL
Server 7.0), porque es necesario para que funcione un sistema hecho con
un generador de código (el cual se aloca si se lo cambiammos). Por lo
menos ya sé por donde abordarlo.

Gracias por el tiempo prestado.

Saludos,
David Ortiz.

"jcac" wrote in message
news:%
Hola David, asi como indicas debe de ser un problema de tu sql server,
lei un anterior post tuyo e indicas que estas en SQL Server 2000 (te iba
a decir que verifiques tu version con un select @@version), entonces no
deberías de tener problemas.

Por lo menos a mi nunca me ha pasado lo que indicas, quizás te falte
algún service pack, no lo se que mas pueda ser.

Bueno quizás a alguien le haya pasado.

Lamento no poderte ayudar

Saludos

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola jcac, gracias por tu molestia.
Resultado : Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 103
A RETURN statement with a return value cannot be used
in this context.

Como podrás ver, el problema no es la conversión de tipos de datos,
sino que es la sintaxis misma, además que pide que declare los
parametros de la función y no se acepta el Return, al parecer uds. si
la han podido crear.
Como dije anteriormente, he revisado en la ayuda, ejemplos, etc. y
debería estar correcta su definición, pero el SQL no la quiere, otra
cosa que he visto, es que cuando le doy al asistente "Crear nueva
función" me genera el script y sin tocarle nada, al darle "chequear la
sintaxis", devuelve el error que "está mal declarado 'CREATE FUNCTION',
ó sea el mismo que me devuelve en la primera línea. Comienzo a creer
que es un problema del SQL Server.

Saludos.


"jcac" wrote in message
news:Ol4$
Hola

Me copie tu funcion esto es lo que tengo espero te sirva

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter FUNCTION dbo.CalculaTiempo(@FechaInicial AS datetime,@FechaFinal
AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Minute) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Hour) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Day) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Month) + ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Year) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

Saludos




"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
¿?

Precisamente, esta función la quiero crear en SQL Server 2000.

Saludos.

"DNC" wrote in message
news:
Recuerda que las funciones fueron introducidas en mssql 2000.


Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any
kind. The entire risk
usage and all it's consequences including data loss and hardware
damage are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se
guarda; solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente,
responder discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola Noldis, gracias por contestar.
Me sigue dando exactamente los mismos errores que al inicio. ¿Qué
otra cosa puede ser?
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 23
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 30
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 37
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 44
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 47
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 52
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 59
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 88
A RETURN statement with a return value cannot be used in this
context.

Saludos!

"Noldis Chumacero" wrote in message
news:%23$
David,

Tienes problemas de conversión, aqui te envío la versión que me
funciona con los cambios

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) -
DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial)
+ @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) -
DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = CAST(@Minute AS VARCHAR(10)) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = CAST(@Hour AS VARCHAR(10)) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = CAST(@Day AS VARCHAR(10)) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = CAST(@Month AS VARCHAR(10))+ ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = CAST(@Year AS VARCHAR(10)) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

SELECT [test_db].[dbo].[CalculaTiempo]('1/2/2006', GETDATE())


Saludos
Ing. Noldis Chumacero Ch.
Dpto. Sistemas, AeroSur.
Santa Cruz de la Sierra - Bolivia

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor
que dos), quiero crear la siguiente función, pero me devuelve una
serie de errores. He revisado la ayuda y también otros ejemplos
de creación de funciones y no doy en que me estoy quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) -
DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) -
DatePart(dd,@FechaInicial) + @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) + @Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) -
DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.





































Respuesta Responder a este mensaje
#13 jcac
12/09/2006 - 18:04 | Informe spam
De nada :D


"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Correcto!, funciona!

Un inmenso 'gracias'

Saludos.

"jcac" wrote in message
news:
Creala la funcion en otra bd que tengas la compatibilidad en 80 y desde
tu bd en 70 la llamas y te funcionará, de esta manera

Select [bd con compatibilidad al 80].dbo.calculatiempo('20060109',
Getdate())

Saludos

:D

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:%
Hola, ya descubrí cual es el problema.
Se debe a que tenemos la compatibilidad de la base de datos en 70 (SQL
Server 7.0), porque es necesario para que funcione un sistema hecho con
un generador de código (el cual se aloca si se lo cambiammos). Por lo
menos ya sé por donde abordarlo.

Gracias por el tiempo prestado.

Saludos,
David Ortiz.

"jcac" wrote in message
news:%
Hola David, asi como indicas debe de ser un problema de tu sql server,
lei un anterior post tuyo e indicas que estas en SQL Server 2000 (te
iba a decir que verifiques tu version con un select @@version),
entonces no deberías de tener problemas.

Por lo menos a mi nunca me ha pasado lo que indicas, quizás te falte
algún service pack, no lo se que mas pueda ser.

Bueno quizás a alguien le haya pasado.

Lamento no poderte ayudar

Saludos

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola jcac, gracias por tu molestia.
Resultado : Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 103
A RETURN statement with a return value cannot be used
in this context.

Como podrás ver, el problema no es la conversión de tipos de datos,
sino que es la sintaxis misma, además que pide que declare los
parametros de la función y no se acepta el Return, al parecer uds. si
la han podido crear.
Como dije anteriormente, he revisado en la ayuda, ejemplos, etc. y
debería estar correcta su definición, pero el SQL no la quiere, otra
cosa que he visto, es que cuando le doy al asistente "Crear nueva
función" me genera el script y sin tocarle nada, al darle "chequear la
sintaxis", devuelve el error que "está mal declarado 'CREATE
FUNCTION', ó sea el mismo que me devuelve en la primera línea.
Comienzo a creer que es un problema del SQL Server.

Saludos.


"jcac" wrote in message
news:Ol4$
Hola

Me copie tu funcion esto es lo que tengo espero te sirva

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial)
+ @Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Minute) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Hour) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Day) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Month) + ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = Convert(Varchar(50), @Year) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

Saludos




"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
¿?

Precisamente, esta función la quiero crear en SQL Server 2000.

Saludos.

"DNC" wrote in message
news:
Recuerda que las funciones fueron introducidas en mssql 2000.


Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of
any kind. The entire risk
usage and all it's consequences including data loss and hardware
damage are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se
guarda; solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente,
responder discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola Noldis, gracias por contestar.
Me sigue dando exactamente los mismos errores que al inicio. ¿Qué
otra cosa puede ser?
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 23
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 30
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 37
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 44
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 47
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 52
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 59
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 88
A RETURN statement with a return value cannot be used in this
context.

Saludos!

"Noldis Chumacero" wrote in message
news:%23$
David,

Tienes problemas de conversión, aqui te envío la versión que me
funciona con los cambios

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) -
DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) -
DatePart(dd,@FechaInicial) + @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) -
DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = CAST(@Minute AS VARCHAR(10)) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = CAST(@Hour AS VARCHAR(10)) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = CAST(@Day AS VARCHAR(10)) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = CAST(@Month AS VARCHAR(10))+ ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = CAST(@Year AS VARCHAR(10)) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

SELECT [test_db].[dbo].[CalculaTiempo]('1/2/2006', GETDATE())


Saludos
Ing. Noldis Chumacero Ch.
Dpto. Sistemas, AeroSur.
Santa Cruz de la Sierra - Bolivia

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor
que dos), quiero crear la siguiente función, pero me devuelve
una serie de errores. He revisado la ayuda y también otros
ejemplos de creación de funciones y no doy en que me estoy
quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) -
DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) -
DatePart(hh,@FechaInicial) + @Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) -
DatePart(dd,@FechaInicial) + @Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) -
DatePart(mm,@FechaInicial) + @Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) -
DatePart(yy,@FechaInicial) + @Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.









































Respuesta Responder a este mensaje
#14 DNC
13/09/2006 - 01:33 | Informe spam
si la compatibilidad es 7 pues servira todo lo que servia en 7 y no lo
introducido en 2k, por eso el comentario anterior.

Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any kind.
The entire risk
usage and all it's consequences including data loss and hardware damage are
with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se guarda; solo
se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente, responder
discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
¿?

Precisamente, esta función la quiero crear en SQL Server 2000.

Saludos.

"DNC" wrote in message
news:
Recuerda que las funciones fueron introducidas en mssql 2000.


Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any
kind. The entire risk
usage and all it's consequences including data loss and hardware damage
are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se guarda;
solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente, responder
discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola Noldis, gracias por contestar.
Me sigue dando exactamente los mismos errores que al inicio. ¿Qué otra
cosa puede ser?
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 23
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 30
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 37
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 44
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 47
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 52
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 59
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 88
A RETURN statement with a return value cannot be used in this
context.

Saludos!

"Noldis Chumacero" wrote in message
news:%23$
David,

Tienes problemas de conversión, aqui te envío la versión que me
funciona con los cambios

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS datetime,@FechaFinal
AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = CAST(@Minute AS VARCHAR(10)) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = CAST(@Hour AS VARCHAR(10)) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = CAST(@Day AS VARCHAR(10)) + ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = CAST(@Month AS VARCHAR(10))+ ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = CAST(@Year AS VARCHAR(10)) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

SELECT [test_db].[dbo].[CalculaTiempo]('1/2/2006', GETDATE())


Saludos
Ing. Noldis Chumacero Ch.
Dpto. Sistemas, AeroSur.
Santa Cruz de la Sierra - Bolivia

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor que
dos), quiero crear la siguiente función, pero me devuelve una serie de
errores. He revisado la ayuda y también otros ejemplos de creación de
funciones y no doy en que me estoy quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.

















Respuesta Responder a este mensaje
#15 David Ortiz
13/09/2006 - 20:46 | Informe spam
Pues, ahora si entiendo el punto, pero cuando hice la pregunta, no sabía que
la puñetera compatibilidad estaba en 7 y por lo que a mi entender, no es lo
mismo tener instalado SQL Server 7 que SQL Server 2000 con una BD en
compatibilidad 7 (a lo mejor estoy equivocado).
Pero como ya está resuelto, todos contentos. :-)

Saludos.

"DNC" wrote in message
news:
si la compatibilidad es 7 pues servira todo lo que servia en 7 y no lo
introducido en 2k, por eso el comentario anterior.

Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any kind.
The entire risk
usage and all it's consequences including data loss and hardware damage
are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se guarda;
solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente, responder
discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
¿?

Precisamente, esta función la quiero crear en SQL Server 2000.

Saludos.

"DNC" wrote in message
news:
Recuerda que las funciones fueron introducidas en mssql 2000.


Cordiales Saludos! ,
Diego.-

<!--Enviar Email: Pega esto en un .htm -->
<!-- INICIO -->
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#100;&#99;&#111;
&#110;&#116;&#105;&#110;&#64;&#65;&#114;&#103;&#101;&#110;&#116;
&#105;&#110;&#97;&#46;&#99;&#111;&#109;">
Diego N. Contin</a>
<!-- FIN -->
Uso del Foro
http://www.mvp-access.com/rubenvigon/foro/

Este mensaje se proporciona TAL CUAL.
Sin ningun derecho o garantia

The documentation is provided to you "as is" without warranty of any
kind. The entire risk
usage and all it's consequences including data loss and hardware damage
are with you.

"En cuestiones de cultura y de saber, solo se pierde lo que se guarda;
solo se gana lo que se da". Antonio Machado

"Tres cosas que son el deber de todos: escuchar humildemente, responder
discretamente y juzgar bondadosamente."
Tríada celta.

"I wish I'd known that before I started writing all this code."
"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola Noldis, gracias por contestar.
Me sigue dando exactamente los mismos errores que al inicio. ¿Qué otra
cosa puede ser?
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 2, Line 23
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 27
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 30
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 37
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 44
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 47
Must declare the variable '@FechaInicial'.
Msg 137, Level 15, State 2, Line 52
Must declare the variable '@FechaFinal'.
Msg 137, Level 15, State 2, Line 59
Must declare the variable '@FechaFinal'.
Msg 178, Level 15, State 1, Line 88
A RETURN statement with a return value cannot be used in this
context.

Saludos!

"Noldis Chumacero" wrote in message
news:%23$
David,

Tienes problemas de conversión, aqui te envío la versión que me
funciona con los cambios

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS
datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial) +
@Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = CAST(@Minute AS VARCHAR(10)) + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = CAST(@Hour AS VARCHAR(10)) + ' Horas, ' +
@Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = CAST(@Day AS VARCHAR(10)) + ' Días, ' +
@Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = CAST(@Month AS VARCHAR(10))+ ' Meses, ' +
@Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = CAST(@Year AS VARCHAR(10)) + ' Años, ' +
@Resultado
END
END

RETURN @Resultado
END
GO

SELECT [test_db].[dbo].[CalculaTiempo]('1/2/2006', GETDATE())


Saludos
Ing. Noldis Chumacero Ch.
Dpto. Sistemas, AeroSur.
Santa Cruz de la Sierra - Bolivia

"David Ortiz" <rortiz-ARROBA-swdeca.com> escribió en el mensaje
news:
Hola a todos, espero que me puedan ayudar (muchos ojos ven mejor que
dos), quiero crear la siguiente función, pero me devuelve una serie
de errores. He revisado la ayuda y también otros ejemplos de creación
de funciones y no doy en que me estoy quivocando.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.CalculaTiempo(@FechaInicial AS
datetime,@FechaFinal AS datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int
DECLARE @Hour int
DECLARE @Minute int
DECLARE @Second int
DECLARE @UltimateDay int
DECLARE @Resultado varchar(50)

SET @Resultado = ''
SET @Year = 0
SET @Month = 0
SET @Day = 0
SET @Hour = 0
SET @Minute = 0
SET @UltimateDay = 0

IF @FechaInicial > @FechaFinal
BEGIN
SET @Resultado = 'Fecha Inicial mayor que Fecha Final'
END
IF @FechaInicial <= @FechaFinal
BEGIN

SET @Minute = DatePart(mi,@FechaFinal) - DatePart(mi,@FechaInicial)
IF @Minute < 0
BEGIN
SET @Hour = @Hour - 1
SET @Minute = @Minute + 60
END

SET @Hour = DatePart(hh,@FechaFinal) - DatePart(hh,@FechaInicial) +
@Hour
IF @Hour < 0
BEGIN
SET @Day = @Day - 1
SET @Hour = @Hour + 24
END

SET @Day = DatePart(dd,@FechaFinal) - DatePart(dd,@FechaInicial) +
@Day
IF @Day < 0
BEGIN
SET @UltimateDay = Day(@FechaInicial)
SET @Month = @Month - 1
SET @Day = @Day + @UltimateDay
END

SET @Month = DatePart(mm,@FechaFinal) - DatePart(mm,@FechaInicial)
+ @Month
IF @Month < 0
BEGIN
SET @Year = @Year - 1
SET @Month = @Month + 12
END

SET @Year = DatePart(yy,@FechaFinal) - DatePart(yy,@FechaInicial) +
@Year

IF @Minute > 0
BEGIN
SET @Resultado = @Minute + ' Minutos'
END

IF @Hour > 0
BEGIN
SET @Resultado = @Hour & ' Horas, ' + @Resultado
END

IF @Day > 0
BEGIN
SET @Resultado = @Day & ' Días, ' + @Resultado
END

IF @Month > 0
BEGIN
SET @Resultado = @Month & ' Meses, ' + @Resultado
END

IF @Year > 0
BEGIN
SET @Resultado = @Year & ' Años, ' + @Resultado
END
END

RETURN @Resultado
END
GO

Saludos,

David Ortiz
El Salvador, C.A.





















email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida