Doubt - SQL Server Query

Asked

Viewed 64 times

0

As I do in sql server to get the records larger than 10 days from the current date, that is, in the current month, I want to get all the records prior to day 01-08. I tried that way, but you didn’t return anything.

SELECT
S.SolID [Chamado],
UC.UsuNome [Cliente],
C.Descricao [Curva ABC],

CONVERT(DATE,S.SolData,103) [Data de Abertura]
FROM Solicitacao S
LEFT JOIN Usuario UC on UC.Usuid = S.UsuIDCliente
LEFT JOIN CurvaABC C on C.CurvaID = UC.CurvaID

LEFT JOIN Usuario U on U.UsuID = S.UsuIDResponsavel
WHERE S.SolStatus <> 9 and CONVERT(DATE,S.SolData,103) = getdate() - 10
and U.UsuIDGrupo = 1151
  • S.Weld is a field of type datetime or varchar() on the bench?

2 answers

2


You can use the DATEDIFF function to perform this comparison. For this, I am considered that the Soldata field is of type DATETIME.

Basically, just put this in your WHERE clause.

DATEDIFF(dd,S.SolData,getdate()) >= 10

It’s the same as doing:

S.SolData <= getdate() - 10

However, the advantage of DATEDIFF is that you can specify that the date interval to be compared is in Day, Month, Year, Hour, Minute, Second, etc.

In the above case, the DATEDIFF is returned to difference, in days (parameter dd), between the SolData and the current date of the Bank. If this difference is >= to 10 days, then the records that meet will be returned.

  • Thanks a lot. Thank you

  • Good morning @Cantoni.. As I always say.. living and learning :) This one I didn’t know.. Thanks for sharing :) Hugs

0

Good afternoon @Renan Bessa

How do I in sql server to get the records larger than 10 days from the current date

TO CATCH ALL DATES LONGER THAN 10 DAYS WOULD BE :

SELECT * FROM clinica.teste
where
(data_nascimento > curdate()+10);
select * from teste;

CURRENT DATE = KURDISH();
CURRENT DATE + 10 DAYS = CURDATE() + 10;
To catch the smaller ones just reverse the signal >
I hope I understand your question :)
Hugs

  • Thanks a lot. Thank you

Browser other questions tagged

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