Solved

Parse DateTime and half hourly period into single DateTime

Posted on 2014-02-10
2
359 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
2 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now