Doubt with DATEDIFF days range between dates

0

I have a query that needs to return the range of days, the problem is that the date fields are set to varchar (30).

  --a data teria que ficar assim
  SELECT DATEDIFF(day,'2015/12/16','2015/12/20') AS DiffDate 
  --data está gravada assim no banco como varchar(30)
  SELECT DATEDIFF(day,'16/12/2015','16/12/2015') AS DiffDate
  select a.*, 
  DATEDIFF(DAY,a.EMISSAO,a.VENCIMENTO) as Dias 
  from TB_DOCUMENTOS_ABERTOS a
  inner join TB_CLIENTE b on a.CODIGOCLIENTE = b.CODIGO
  ORDER BY b.RAZAO

Error: Message 241, Level 16, State 1, Line 1 Conversion failed when converting date and / or time from character string.

    
asked by anonymous 16.03.2016 / 15:25

1 answer

0

Using the convert function like this:

 REPLACE(CONVERT(DATE, @data, 103), '-','/')

Then calculate DIFF

SELECT DATEDIFF(DAY,REPLACE(CONVERT(DATE, emissao, 103), '-','/'),REPLACE(CONVERT(DATE, vencimento, 103), '-','/')) AS DiffDate
    
16.03.2016 / 16:26