Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

Convertir campo

Estas en el tema de Convertir campo en el foro de Mysql en Foros del Web. Como convierto un campo q es recibido en un strored procedure como varchar a integer...
  #1 (permalink)  
Antiguo 21/04/2009, 20:07
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Convertir campo

Como convierto un campo q es recibido en un strored procedure como varchar a integer
  #2 (permalink)  
Antiguo 21/04/2009, 20:15
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Convertir campo

Lo puedes hacer con la función cast.

select cast(var_varchar as unsigned)

Un saludo.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #3 (permalink)  
Antiguo 21/04/2009, 20:19
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

Y para asignarle esta conversion a una variable ????
  #4 (permalink)  
Antiguo 21/04/2009, 20:24
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Convertir campo

Asumo que es igual a como te dije en un post pasado.

Código sp:
Ver original
  1. declare entero integer;
  2. select cast(variable_char as unsigned) into entero;

Un saludo.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #5 (permalink)  
Antiguo 21/04/2009, 20:39
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

me da error de sintaxis........
  #6 (permalink)  
Antiguo 21/04/2009, 20:42
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Convertir campo

Que error te muestra? como lo haces?
Cual es el store procedure?

La función cast lo que hace es convertir a otro tipo de dato.
select cast('2' as unsigned) te pasa el '2' varchar a 2 entero.

trata de ser un poco mas explicito chicorio.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #7 (permalink)  
Antiguo 21/04/2009, 20:47
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

CREATE DEFINER=`teletaxusr`@`%` PROCEDURE `Costs_Calls`(begindate nvarchar(10), enddate nvarchar (10), firstExt varchar (15),LastExt varchar (15), CallType int )
BEGIN


Declare starDatecall datetime;
Declare EndDateCall datetime;
Declare BeginExt integer;

select cast(concat(begindate ,' 00:00:00') AS datetime) into starDatecall;
select cast(concat(enddate ,' 23:59:59') AS datetime) into EndDateCall ;
select cast(firstExt as unsigned) into BeginExt;

Ahi despues sigue el resto del sp el error solo dice error de sintaxis en la linea 16

Última edición por chicorio; 21/04/2009 a las 21:02
  #8 (permalink)  
Antiguo 21/04/2009, 20:51
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

CREATE DEFINER=`teletaxusr`@`%` PROCEDURE `Costs_Calls`(begindate nvarchar(10), enddate nvarchar (10), firstExt varchar (15), LastExt varchar (15), CallType int )
BEGIN


/* Creted by Calos Murillo April 2009

calaculate cost of customers calls */


Declare starDatecall datetime;
Declare EndDateCall datetime;
Declare BeginExt integer;

select cast(concat(begindate ,' 00:00:00') AS datetime) into starDatecall;
select cast(concat(enddate ,' 23:59:59') AS datetime) into EndDateCall ;
select cast(firstExt as unsigned) into BeginExt;

DROP TABLE IF EXISTS Calc;
create temporary table Calc
(Call_Date datetime, cost float (10,3), extension varchar (15), number varchar (10), Call_type varchar (15));


if CallType = 0 then

insert into Calc (Call_Date, cost, extension,number, Call_type )
select ctime,( CEIL(duration /60 ) *
Case when LEFT(calling_num , 1) = '2' and CHARACTER_LENGTH(calling_num )= 8
then 0.01
WHEN LEFT(calling_num , 1) = '8' and CHARACTER_LENGTH(calling_num )= 8
then 0.10
WHEN CHARACTER_LENGTH(calling_num )>= 10
then 0.16
end ) cost, dialed_num, calling_num , ('in')
from tbCalls
where ctime >= starDatecall
and ctime <= EndDateCall
and dialed_num between firstExt and lastExt
and duration > 0
and calling_num <> '';

insert into Calc (Call_Date, cost, extension,number, Call_type )
select ctime,( CEIL(duration /60 ) *
Case When LEFT(dialed_num, 1) = '2' and CHARACTER_LENGTH(dialed_num )= 8
then 0.05
When LEFT(dialed_num, 1) = '8' and CHARACTER_LENGTH(dialed_num)= 8
then 0.10
When LEFT(dialed_num, 1) = '1' and CHARACTER_LENGTH(dialed_num)=12
then 0.16
When LEFT(dialed_num, 2) = '00'
then 0.50
When CHARACTER_LENGTH(dialed_num)=10
then 0.16
else
0.05
End)cost, calling_num, dialed_num, ('out')
from tbCalls
where ctime >= starDatecall
and ctime <= EndDateCall
and calling_num between firstExt and lastExt
and duration > 0;

end if;


if CallType = 1 then

insert into Calc (Call_Date, cost, extension,number, Call_type )
select ctime,( CEIL(duration /60 ) *
Case when LEFT(calling_num , 1) = '2' and CHARACTER_LENGTH(calling_num )= 8
then 0.01
WHEN LEFT(calling_num , 1) = '8' and CHARACTER_LENGTH(calling_num )= 8
then 0.10
WHEN CHARACTER_LENGTH(calling_num )>= 10
then 0.16
end ) cost, dialed_num, calling_num , ('in')
from tbCalls
where ctime >= starDatecall
and ctime <= EndDateCall
and dialed_num between firstExt and lastExt
and duration > 0
and CHARACTER_LENGTH(calling_num )> 4;
end if;






if CallType = 2 then



insert into Calc (Call_Date, cost, extension,number, Call_type )
select ctime,( CEIL(duration /60 ) *
Case When LEFT(dialed_num, 1) = '2' and CHARACTER_LENGTH(dialed_num )= 8
then 0.05
When LEFT(dialed_num, 1) = '8' and CHARACTER_LENGTH(dialed_num)= 8
then 0.10
When LEFT(dialed_num, 1) = '1' and CHARACTER_LENGTH(dialed_num)=12
then 0.16
When LEFT(dialed_num, 2) = '00'
then 0.50
When CHARACTER_LENGTH(dialed_num)=10
then 0.16
else
0.05
End)cost, calling_num, dialed_num, ('out')
from tbCalls
where ctime >= starDatecall
and ctime <= EndDateCall
and dialed_num > 4
and calling_num >=firstExt
and calling_num <=lastExt
and duration > 0;

end if;



select * from Calc
order by extension,Call_Date;


END

Última edición por chicorio; 21/04/2009 a las 20:59
  #9 (permalink)  
Antiguo 21/04/2009, 21:01
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Convertir campo

Donde te señala el error de sintaxis?
No parece ser en la conversión a integer.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #10 (permalink)  
Antiguo 21/04/2009, 21:06
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

mmm solo cuando agrego esa linea me da el error si la quito me permite guardar los cambios
  #11 (permalink)  
Antiguo 21/04/2009, 21:15
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

Listo funciono gracias
  #12 (permalink)  
Antiguo 21/04/2009, 21:16
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Convertir campo

cual era el error?
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #13 (permalink)  
Antiguo 22/04/2009, 13:52
 
Fecha de Ingreso: abril-2009
Mensajes: 67
Antigüedad: 15 años
Puntos: 0
Respuesta: Convertir campo

El erro estaba en la transformacion cuando hacia el cast

select cast(firstExt as unsigned) into BeginExt; por error en el codigo q tenia en mi pc escribi integer en lugar de unsigned
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:00.