I have a table which contains a value for every 30 minutes in a month, e.g.
20/03/2010 00:00 12
20/03/2010 00:30 14
etc
All of this data is stored in GMT
I need to do a select on this table for the data in bst/clock time
for example
select *
from tbl
where dt between '01 April 2010' and '30 April 2010 23:30'
when in BST as the date range above is the dates need to be converted
I also need a way of taking the changeover hour and the end of March and October into account
Unfortunatly I cannot upgrade SQL server
Is there any way that I can do this in SQL for SQL Server 2000?
something like a function I could do?
select *
from tbl
where fnConvertToClockTime(dt) between '01 April 2010' and '30 April 2010 23:30'
When in GMT the function would return the exact date from the table
Cheers
Rupert
I have the same problem and am investigating the use of Calendar tables for such convserions. Store all dates in UTC time then convert as necessary, I'm probably going to create VIEWS for the different time zones to avoid parameter passing but I guess a stored procedure would also work.
I found some useful information here: how-do-i-convert-local-time-to-utc-gmt-time
And an associated article on Calendar tables here: why-should-i-consider-using-an-auxiliary-calendar-table
Related
I have the following query that pulls all records from a Redshift table from January 1st of the current year through the final date of the most recent, full quarter.
SELECT *
FROM table
WHERE date_value BETWEEN DATE_TRUNC('year',getdate()) AND DATE_TRUNC('quarter',dateadd(day,-1,getdate()));
I now want to create a period-over-period comparison query that returns all records for the previous n months. Ex. if the first query returns all records for Jan - Jun 2022, this query will return all records for Jul - Dec 2021.
Here is what I have so far, however it currently returns Jan - Jun 2021 instead of the desired date range. I've tried playing around with DATEDIFF() instead of DATEADD() but haven't had any luck with that either. Any help is much appreciated.
SELECT *
FROM TABLE
WHERE date_value BETWEEN DATE_TRUNC('year',dateadd(year,-1,getdate())) AND DATE_TRUNC('quarter',dateadd(year,-1,getdate()));
I have a query which uses needs to know how many days passed since 1st of January in the current year.
Which means that if the query runs for example in:
2nd Jan 2017 than it should return 2 (as 2 days passed since 1st Jan
2017).
10th Feb 2016 than it should return 41 (as 41 days passed since 1st
Jan 2016).
basically it needs to take Current Year from Curent Date and count the days since 1/1/(Year).
i have the current year with: SELECT EXTRACT(year FROM CURRENT_DATE);
I created the 1st of Jan with:
select (SELECT EXTRACT(year FROM CURRENT_DATE)::text || '-01-01')::date
How do I get the difference from this date to Current_Date?
Basically this question can be Given two dates, how many days between them?
Something like age(timestamp '2016-01-01', timestamp '2016-06-15') isn't good because I need the result only in days. while age gives in years,months and days.
An easier approach may be to extract the day of year ("doy") field from the date:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE);
date_part
-----------
41
And if you need it as a number, you could just cast it:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE)::int;
date_part
-----------
41
Note: The result 41 was produced by running the query today, February 9th.
Given two dates, how many days between them
Just subtract one from the other.
In your case you could just round the current_date to the start of the year and subtract that from the current date:
select current_date - date_trunc('year', current_date)::date
The ::date cast is necessary to get the result as an integer, otherwise the result will be an interval.
Another solution is to use DATEDIFF
SELECT DATE_PART('day', now()::timestamp - '2016-01-01 00:00:00'::timestamp);
I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.
Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.
I want to make a payment to members on 1st may 2012 and i want to retrieve the data from the date between 1st april 2011 to 31st march 2012. How is it possible thru a query if i am a table (xyz) which maintains that on which date that person has become a member
select * from xyz where datecoljoined between '2011-04-01' and '2012-03-31'
Complying with British settings:
select * from xyz where datecoljoined between '20110401' and '20120331'
I have a query, Im trying to select the maximum value from the summer period (nov-april down here) but it only gives me values from nov-dec with this query. Any ideas why?
SELECT TOP 10 Value, DateTime
FROM history
WHERE Tagname = #Tag
AND
((DateTime >= #StartYear AND DateTime < #StartWinter)
OR
(DateTime >= #FinishWinter AND DateTime < #FinishYear))
ORDER BY Value DESC
(DateTime >= startYear AND datetime < startwinter) gives you all results between jan and april 2009.
(Datetime > finishwinter and datetime < finishyear) gives all results in nov dec 09.
So, you're selecting top 10 from Jan Feb March April Nov Dec 2009. If that's what you want to select from, and you're only getting values in Nov Dec 2009, check to see that there should be values in the other months?
If #startwinter isn't year-sensitive you might also get jan-apr 2010.
Shouldn't you use a 'ORDER BY' when using 'TOP 10'?
And what locale do you live in, or, to rephrase it: what are reasonable dates for (#StartYear, #StartWinter, #FinishWinter, #FinishYear)
In Europe I expect:
StartYear = 2010-01-01
FinishYear= 2010-12-31
StartWinter=2010-12-20 (about)
FinishWinter=2010-03-20 (about)
So the first period would go from 2010-01-01 to 2010-12-20 (about) and the second from March 2010 to End of year.
So this would include the whole year, and most of it, from 03.20 to 12.20 double.
Hey thanks for the help everyone, it seems this is a problem with our historian (a linked db from sql server) so Ill take the issue up with them. I tried the query on a regular mssql db and it seeems fine...