Doubt with DATEDIFF days interval between dates

Asked

Viewed 208 times

0

I have a query that I need to return the interval of days, the problem is that the date fields are defined as 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, Status 1, Line 1 Conversion failed when Converting date and/or time from Character string.

1 answer

0


Using the function Convert thus:

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

Then calculate the DIFF

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.