?
Solved

Parse DateTime and half hourly period into single DateTime

Posted on 2014-02-10
2
Medium Priority
?
401 Views
Last Modified: 2014-02-10
Hi,

I have a View that returns half hourly data samples and a column called ReadingDate which is the sanple days date (i.e. 2011-11-09 00:00:00) and a 'Period' column which returns the half hourly period (i.e. 1-48). I need to combine these 2 values within a Stored procedure to give the sample DateTime.

Attached is the sample data from the table and also part of my SP where I need to use the parsed out DateTime. Currently I'm using the ReadingDate directly, but I need to be inserting the parsed DateTime instead throughout this query instead of ReadingDate or timestamp (timestamp is no longer in use, but could be the name of the parsed DateTime).

INSERT INTO #ReportData (Timestamp, Hundredths, Data, Datestamp, CalendarDate, BaseloadData,Occupancy, OAT, SpaceTemp, Widgets,ManualRead,BaseloadManualRead,ManualConsumption)
SELECT ReadingDate, 0, Readingkwh
       ,case when @reportselection = 'Y' then convert(char(6),timestamp,112)+'01' else convert(char(8),timestamp,112) end
       ,convert(char(8),timestamp,112)
       ,0
       ,case when @occtl = 0 then NULL else (SELECT top 1 convert(int,data) 
                 FROM HistorianDB.dbo.TLData O
                 WHERE O.TLInstance = @Occtl
                 AND O.timestamp <= M.timestamp
                 AND O.Type=0 
                 AND isnumeric(O.data) = 1
                 ORDER BY O.timestamp desc) end
       ,case when @oattl = 0 then 0 else (SELECT top 1 convert(real,data) 
         FROM HistorianDB.dbo.TLData O
         WHERE O.TLInstance = @OatTl
         AND O.timestamp between @sdate_trimmed and M.timestamp
         AND O.Type=0 
         AND isnumeric(O.data) = 1
         ORDER BY O.timestamp desc) end
       ,case when @Spacetl = 0 then 0 else (SELECT top 1 convert(real,data) 
         FROM HistorianDB.dbo.TLData O
         WHERE O.TLInstance = @SpaceTl
         AND O.timestamp between @sdate_trimmed and M.timestamp
         AND O.Type=0 
         AND isnumeric(O.data) = 1
         ORDER BY O.timestamp desc) end
		 ,0,
         MeterReading
         ,0,Consumption

FROM ERM.dbo.EnergyDisplayValues M
WHERE MeterID = @MeterID
AND ((m.ReadingDate between @sdate_trimmed and @edate) or(m.ReadingDate between @sdate2 and @edate2))
ORDER BY m.ReadingDate,m.Period

Open in new window


SampleData.txt
0
Comment
Question by:wint100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 2000 total points
ID: 39846878
Sorry, a quick answer which will hopefully set you on the right track.

Now it depends whether you want the first minute of the half hour or the last, I am assuming the first would would be

dateadd(minute, (Period-1)*30, ReadingDate)

but if you need the last it would be

dateadd(minute, Period*30, ReadingDate)

HTH
0
 
LVL 1

Author Comment

by:wint100
ID: 39846900
Great, simple but exactly what I needed.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question