Solved

Parse DateTime and half hourly period into single DateTime

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a step to a system backup job 6 36
point in time restore in SQL server 26 46
Change this SQL to get all nodes 3 40
T-SQL: I need to add an index on a field 5 27
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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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