Domanda

On my table I have a "Datemodified" column. I was to be able to pull results based on that Column. I want to be able to pull anything from 6am to 6pm. This query needs to be able to be used everyday, so the ways I've seen, use a specific date and I can't get any of the queries I've looked for to work for my specific case.

Here is what I've gotten to work so far, it pulls the right hour time frame, but I need it to run only for the current day. Thanks all!

SELECT 
  Assignment
  , Datemodified
  , General
  , IncNumber
  , NextSteps
  , PDCRStatus
  , RootCause
  , Status
  , Summary
  , Timings
  , UserID 
FROM 
  Turnover 
WHERE 
  DATEPART(HOUR, datemodified) between 06 and 18 
  --AND datemodified = GETDATE()
È stato utile?

Soluzione

Convert your datemodified field into a date without time and do the same to getdate(), then you can compare them...

CONVERT(NVARCHAR(50),datemodified,103) = CONVERT(NVARCHAR(50),GETDATE(),103)

so you get:

SELECT Assignment, Datemodified, General, IncNumber, NextSteps, PDCRStatus, RootCause, Status, Summary, Timings, UserID FROM Turnover WHERE DATEPART(HOUR, datemodified) between 06 and 18 AND CONVERT(NVARCHAR(50),datemodified,103) = CONVERT(NVARCHAR(50),GETDATE(),103)

Altri suggerimenti

something like this would work:

SELECT Assignment, Datemodified, General, IncNumber, NextSteps, PDCRStatus, RootCause, Status, Summary, Timings, UserID 
FROM Turnover 
where (Datemodified >= dateadd(hh, SELECT CAST( CONVERT( CHAR(8), GetDate(), 112) AS DATETIME),  6) and  
   (Datemodified <= dateadd(hh, SELECT CAST( CONVERT( CHAR(8), GetDate(), 112) AS DATETIME), 18) )
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top