Query with sql query using time interval

Asked

Viewed 174 times

0

I have a Job in sql server that runs twice a day, 06:00 and 18:00, it has an sql query where I take the date of the process and compare with the date of the day. It turns out that if you have a new change after 18:00, these will not be updated because it will be a new date after 24:00. How could I make up this time in hours on the query by adding this result? I could change the job schedule but the client thinks no, that I wanted another solution, I can’t see a solution. The c.dtHrProcess field has the date in the format : 2016-11-23 04:38:10.307

Segue a consulta 
SELECT DISTINCT p.codInterno,
                CASE
                    WHEN f.idFabricante IS NULL THEN 99999999999999
                    ELSE f.idFabricante
                END AS fabri,
                '0', 
                p.descResumida, 
                p.descProduto,
                p.descProduto,
                p.codElemento,
                GETDATE(),
                GETDATE(),
                0,
                0  
    FROM    [192.168.200.87].[siac].[dbo].[IntPluProduto] p
        INNER JOIN  [192.168.200.98].[siac].[dbo].[IntControle] c on c.idProcesso = p.idProcesso
        LEFT JOIN [192.168.0.150].[tlmark].[dbo].[Produto] vanProduto on vanProduto.CdPrdSAP = p.codInterno
        LEFT JOIN tbFabricante f on f.idFabricante = CAST(vanProduto.CdFbr AS INT)
    WHERE  (select COUNT(*) from tbProduto where idSAP = p.codInterno) = 0
    AND vanProduto.cdFbr <> 'NNN0'
    AND CAST(c.dtHrProcesso AS DATE) = CAST(GETDATE() AS DATE)
    AND c.codModulo = 14
    AND vanProduto.ICMSubst <> 'T'
  • Have you thought about saving the date/time of the last execution of JOB in the database? So you could filter only processes that have an equal or longer date (>=) to the last execution date.

  • How could I do that?

1 answer

1


Change the JOB query that is set to run at 06:00 to filter the current date - 1:

CAST(c.dtHrProcesso AS DATE) > DATEADD(ss, 64800, DATEADD(d, -1, CAST(CONVERT(varchar(10), GETDATE(), 103) AS smalldatetime))) OR CAST(c.dtHrProcesso AS DATE) = CAST(GETDATE() AS DATE)

The above filter does nothing but, starting from the current date, subtracts 1 day, and after adding 18 hours (64800 seconds).

For example, for the current date 27/12/2016 06:00:00:

  • Subtract 1 day: 26/12/2016 06:00:00
  • Format result to: 26/12/2016 00:00:00
  • Add 64800 seconds (18hrs): 26/12/2016 18:00:00

Another way of doing and that I would use, would be to each execution of JOB, update a table with the date/ time of the last execution of JOB.

For example, create a Jobatu table, with a jobData field (DATETIME). When you run JOB, update the date field to the current date/time:

UPDATE JobAtu SET jobData = GETDATE()

Every time JOB is executed, instead of using the clause:

CAST(c.dtHrProcesso AS DATE) = CAST(GETDATE() AS DATE)

utilize:

CAST(c.dtHrProcesso AS DATE) >= SELECT ISNULL(jobData, CAST(GETDATE() AS DATE)) FROM JobAtu
  • AND CAST(c.dtHrProcess AS DATE) > DATEADD(ss, 64800, DATEADD(d, -1, CAST(CONVERT(varchar(10), GETDATE(), 103) AS smalldatetime))) Msg 242, Level 16, State 3, Line 1 The Conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value value.

  • Help me in this conversion problem, thank you

  • What is the return of the call CONVERT(varchar(10), GETDATE(), 103)?

  • The return is: 27/12/2016

  • And if you do SELECT CAST(CONVERT(varchar(10), GETDATE(), 103) AS smalldatetime)?

  • ai i have error: The Conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.

  • Solution: -test, previous date from 18:00 hours SELECT DATEADD(ss, 64800, DATEADD(d, -1, CAST(CONVERT(varchar(10), GETDATE(), 120) AS smalldatetime)))

  • E.Thomas: Using expressions using data types (date, datetime etc) in T-SQL requires some knowledge and experience. Again you post a wrong suggestion to @itasouza, so much so that he created a third topic.

  • Please @Josédiz. The topic he created new is because he is using another language not Brazilian. By your answer you can see that your experience is about copying answers from others, editing them and posting them as your own. My suggestion is not wrong for the language that I’m using. I really think you need to improve on the "knowledge and experience" section. I stopped here.

  • I thank you all for your help, Thomas, thanks man!

  • @Josédiz I will not give guesses in [tag:sql-server] but when there are different opinions the best is to have several responses to the community vote and signal what was useful.

Show 6 more comments

Browser other questions tagged

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