Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parse DateTime and half hourly period into single DateTime

Posted on 2014-02-10
2
Medium Priority
?
415 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

971 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