I have a table that contains a column of type TIMESTAMP (which is stored in UTC). My server is currently in timezone 'America/New_York'. I have the following query:
SELECT * FROM table1 WHERE hour(time) > 5;
Which returns the expected output. I then want to change the server's timezone to something else (doesn't matter what) and I want to get the same output as when it was 'America/New_York'. Basically, I want a query that returns all rows where the hour of time in timezone 'America/New_York' is greater than 5, regardless of what the current server timezone is.
I know I could use:
SELECT * FROM table1 WHERE hour(convert_tz(time, ##session.time_zone, 'America/New_York')) > 5;
But I don't want to use variables (##session.time_zone) because I want this SELECT to be included in a view and views don't support variables.
Is there a way to force a TIMESTAMP to be converted to a specific timezone regardless of the current server's timezone?
UPDATE: I found a way to create a function and then call that function from within the view:
create function getTimeZone() returns VARCHAR(50) DETERMINISTIC NO SQL return ##session.time_zone;
This works fine, but I feel like it's a bit of a hack.
Related
i have a MySQL's db with 2 DATE columns and when i add a registry the date is saved like this 2018/07/30. The problem is that when i make a get call to the db the value comes like this"2018-07-30T03:00:00.000Z"
I do not need the time part, and i think that the DATE type i have set to the column should not return this kind of value.
I have tried this but it does not work:
"SELECT * FROM patrones WHERE DATE_FORMAT(bd_fRecepcion, '%Y-%m-%d') WHERE bd_SI = 'Si' "
How could i do to get retuned the date part only?
Thanks for your help.
I am using two date time parameters in the report. When I use the same parameters in the query, no results are shown. Moreover if I use date parameters then the results are shown but not all records are shown.
I also executed the same query (with date time) in MySQL and the results are shown, I think there is no mistake in the query.
I tried many things. The query is some what like :
SELECT * FROM abc WHERE datetime BETWEEN
date_format($P{fromdate}, '%d-%c-%y 00:00:00')
AND date_format($P{todate}, '%d-%c-%y 23:59:59')
Try without data_format function to check whether error in query or in parameters.
SELECT *
FROM abc WHERE datetime BETWEEN
$P{fromdate} AND $P{todate}
Is there a way to trim off the timestamp in a DB2 date function?
Have DB2 select statement where I'mm selecting a date form the databease and saving it to a variable. I then use that variable as a parameter for another db2 select by adding 30days to it but I don't think it agrees with the timestamp that it is adding to the end.
Select business_date From DB2INST1.BusDate Where key = 0
There is no timestamp in the database for this date but its adding '12:00:00AM' to the end
it saves this select into a variable and I use it in another select here
where expirdate > (DATE(#BusDate) + 30 DAYS)
I get this error:
{"ERROR [428F5] [IBM][DB2/AIX64] SQL0245N The invocation of routine \"DATE\" is ambiguous. The argument in position \"1\" does not have a best fit."} System.Exception {IBM.Data.DB2.DB2Exception}
Select business date as a varchar/string
Select varchar_format(business_date,'YYYY-MM-DD')
then do this to use it later, convert it to a date then use a date function to add 30days to it:
(DATE(to_date(#BusDate, 'YYYY-MM-DD') + 30 DAYS))
Try
where expirdate > DATE(#BusDate + 30 DAYS)
Is the first SELECT statement and the second SELECT Statement part of same stored procedure? Are you storing the resulting date in any .Net variable? If you are storing it in a .Net DateTime variable, my suggestion is to do the date addition operation in .Net code itself. And then remove the time part from the variable before passing to the database.
Take a look at this too: Datetime field overflow with IBM Data Server Client v9.7fp5
If both the SELECT statements were part of a stored procedure and there is no .Net DateTime variable invloved, then it is a different story.
I have a table with a DATETIME field called date_created, and need to check for some data with this kind of query:
SELECT * FROM table WHERE UNIX_TIMESTAMP(date_created) + $number < UNIX_TIMESTAMP()
Is it possible to do this without the UNIX_TIMESTAMP() function? Maybe with NOW())?
And if it is, will it be faster?
Is it possible to do this without the UNIX_TIMESTAMP() function? Maybe with NOW())?
Not exactly. Whilst you could do the following, it won't achieve the exact same results:
SELECT * FROM `table` WHERE date_created + INTERVAL $number SECOND < NOW()
The reason is that, unlike TIMESTAMP, DATETIME is neither intended nor capable of representing a specific instance in time; rather, it effectively represents the display of a calendar/clock (not the same thing).
When using the above query, which does not go via a UTC timestamp, the comparison is merely a question of whether a clock right now would show a date/time later than that which was recorded in the database (plus $number seconds).
However, when converting to a UTC timestamp, the timezone of the respective clock displays become relevant and because many timezones are not constant (e.g. they often move around for daylight savings), multiple DATETIME values could give rise to the same UTC timestamp.
For example:
CREATE TABLE `table` (date_created DATETIME);
INSERT INTO `table` VALUES ('2012-10-28 01:00:00'), ('2012-10-28 02:00:00');
Then compare the results of the two queries when run at 2012-10-28 02:00:00 in the UK:
Your original query:
SET SESSION time_zone = 'Europe/London';
SELECT *
FROM `table`
WHERE UNIX_TIMESTAMP(date_created) + 100
< UNIX_TIMESTAMP('2012-10-28 02:00:00');
The alternative query above:
SELECT *
FROM `table`
WHERE date_created + INTERVAL 100 SECOND
< '2012-10-28 02:00:00';
And if it is, will it be faster?
Probably (a lexicographic order for the comparison will suffice, versus parsing & converting the DATETIME values to UTC according the session timezone's rules followed by subtraction and sign inspection), but I'd advise performing your own benchmarks.
Using eval expression in WHERE clause should be avoided if possible. It prevents correct utilization of indexes. If possible do the math in code and send the values as query parameters.
I have quite a complex MySQL query with a problem but I have narrowed the problem down to the SQL below. The problem is that the MySQL date functions (WEEK, YEAR etc.) don't accept the datetime stored in a user variable.
SELECT
#test := datetime
,datetime
FROM `agenda`
WHERE YEAR(#test) = 2011
This doesn't give me any results, however, the following SQL gives me results:
SELECT
#test := datetime
,datetime
FROM `agenda`
WHERE YEAR(datetime) = 2011
(datetime is a fieldname in the agenda table.)
What is the problem here?
In the first query you're attempting to set #test equal to the datetime fields value based on a WHERE clause that is itself referring to the value of #test.
Unless #test has a value up front then you can't expect this to produce any meaningful results.