1

I have table in Ms SQL Server 2005 (2008) in following structure

ID Name     AddDate                   AmendDate                 Status
-----------------------------------------------------------------------
1  Jason    2012-09-03 08:01:00.000   2012-09-03 14:02:00.000   Amended
2  Robert   2012-09-03 08:05:00.000   NULL                      New
3  Celia    2012-09-03 08:10:00.000   2012-09-03 14:02:00.000   Amended
4  Jason    2012-09-03 14:02:00.000   NULL                      New
5  Robert   2012-09-03 14:03:00.000   2012-09-03 20:02:00.000   Amended
6  Celia    2012-09-03 14:07:00.000   2012-09-03 20:02:00.000   Amended
7  Jason    2012-09-03 20:00:00.000   NULL                      New
8  Robert   2012-09-03 20:02:00.000   NULL                      New
9  Celia    2012-09-03 20:04:00.000   NULL                      New

routine runs three times per day as snapshot,

please how can I by using SQL Query

  1. select all record from morning (or evening) for any of days

  2. how can I replace (ISNULL(AmendDate, '2012.09.04')) for column AmendDate where is Null value, in the case when I walking in history, for example returns all row that AddDate <= '2012-09-01' and AmendDate >= '2012-09-02'

  3. question is about SELECT - FROM - WHERE - only

5
  • what do you mean by "walking in history"? also, do you want to consider null AmendDate as 2012.09.04? Commented Sep 4, 2012 at 7:24
  • @Vikdor "walking in history" I meaning resultset from (for example) morning from january, april or e.i. ..., and in the case that value is NULL I want to add NOW() or DATE() +1, or better is use AmendDate >= '2012-01-15' OR AmendDate IS NULL Commented Sep 4, 2012 at 7:29
  • Sorry, I still couldn't get it, would you be able to add the query with select list and where clauses and then tell what you intend to get the output as? Commented Sep 4, 2012 at 7:32
  • @Vikdor I want to determine if Record is Amended or not, maybe replace NULL to DATE +1 is wrong way, sorry for my Navajo language Commented Sep 4, 2012 at 7:40
  • Since amendDate carries null for records that are not amended, you filtering criteria would be amendDate IS NOT NULL to exclude records that are not amended. Hope I got you right this time :) Commented Sep 4, 2012 at 7:42

3 Answers 3

1

To select all records from morning/evening for any of days

select *
from Table t
where t.AddDate > '2012-09-03' 
      and t.AddDate < '2012-09-04'
      and DatePart(HOUR, t.AddDate) < 12 -- Morning
      -- and DatePart(HOUR, t.AddDate) > 18 -- Evening

For the second part (given that the requirement is not very clear) I think you can use Coalesce function with a CTE

;WITH FullData(Id, Name, AddDate, AmendDate, Status)
     AS (SELECT t.Id,
                t.Name,
                t.AddDate,
                COALESCE(t.AmendDate, '2012-09-04'),
                Status
         FROM   Table t)
SELECT *
FROM   FullData d
WHERE  d.AddDate <= '2012-09-01'
       AND t.AmendDate >= '2012-09-02' 
Sign up to request clarification or add additional context in comments.

Comments

1

For the first one, you can use the DATEPART function on the date to extract the hour and apply your criteria on whether you are interested in the records added in the morning or in the evening.

DATEPART(hh, AddDate) BETWEEN 0 AND 12 -- Morning.

Second question is not clear :(

Comments

1

Do you want something like this for the 2. question? I used a parameter because it is easier to show:

  DECLARE @AddDate datetime

    SELECT *
    FROM yourTable
    WHERE 
    AddDate <= @AddDate 
    and AmendDate >= ISNULL(AmendDate,DATEADD(day, 1, @AddDate))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.