Solved

Parse DateTime and half hourly period into single DateTime

Posted on 2014-02-10
2
374 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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