Server time is not aligning with local time - sql

I have been using date_default_timezone_set('Asia/Manila'); in my website and it echoes out the correct time of my time and date but when I uploaded it to my server the curdate() and now() function gets the time in the server. How will I know how much time should I add or subtract to my sql so that it would align in the server time? I have already tested echoing the time in my website but it only shows the correct time and not the server time. Is there a way to know the time in the server?
EDIT
I am making a filter for my records in my database. Those are today's date, week, month, and past 3 months. I have successfully done that but the time in my localhost is not aligned with the server time that's why when I uploaded it into the server it only shows the records of today after 1pm in the afternoon. I tried adding DATE_SUB(CURDATE(), INTERVAL 13 HOUR) but it doesn't work. 13 hours because I remembered adding 13 hours so that the time showed in the table is aligned with the server time. But then when I added 24 hours it shows the records in the morning but in the afternoon it doesn't show any records at all. Then I tried adding 20 hours to 23 hours but no records was shown both in the morning and in the afternoon. How can I see what is the time of the server so that I can easily add or subtract hours interval so that the records will be shown in the corresponding filter?
This is my whole sql
SELECT *
FROM `report`
WHERE DATE(`Timestamp`) = DATE_ADD(CURDATE(), INTERVAL 24 HOUR)
AND ( Employee_ID IN (SELECT T1.Employee_ID
FROM `employee` T1
WHERE T1.Supervisor_ID = '".$id."'
)
OR Employee_ID IN (SELECT T2.Manager_ID
FROM `branches` T2
WHERE T2.Manager_ID = '".$id."'
)
OR Employee_ID = '".$id."'
)

Related

how to get data for last calender week in redshift

I have a below query that I run to extract material movements from the last 7 days.
Purpose is to get the data for the last calender week for certain reports.
select
*
From
redshift
where
posting_date between CURRENT_DATE - 7 and CURRENT_DATE - 1
That means I need to run the query on every Monday to get the data for the former week.
Sometimes I am too busy on Monday or its vacation/bank holiday. In that case I would need to change the query or pull the data via SAP.
Question:
Is there a function for redshift that pulls out the data for the last calender week regardless when I run the query?
I already found following solution
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
But this doesnt seem to be working for redshift sql
Thanks a lot for your help.
Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.
So this might work for you. It filters rows from the Sunday before last, up until but not including, the last Sunday, and so gets a full week.
SELECT id
FROM table1
WHERE date >= DATE_TRUNC('week', NOW()) - INTERVAL 1 WEEK
AND date < DATE_TRUNC('week', NOW())
Pro tip: Every minute you spend learning your DBMS's date/time functions will save you an hour in programming.

Drupal: getting exact date from created column in ddbb

So I'm trying to extract the created, access and login dates from the users in the database of a Drupal site. But the values are like: 1377783381, 1384248801...
I have tried to do this in the SQL:
SELECT DATE_SUB(NOW(), INTERVAL us.created SECOND), us.name
FROM users us
But it returns dates from 1970 - 1974 and dates should go from 2013 - 2018 approx.
The truth is that I don't know what represents that numbers, but they aren't seconds.
Perhaps the values are milliseconds. Try this:
SELECT DATE_SUB(NOW(), INTERVAL us.created/1000 SECOND), us.name
FROM users us;
For some reason, MySQL supports seconds and microseconds, but not milliseconds.
Actually, as I think about it, subtracting a millisecond value from the current time doesn't make sense. More likely, this is a unix time value:
SELECT FROM_UNIXTIME(us.created), us.name
FROM users us;

while accessing remote database which is in different timezone i'm not able to find the last 10 min records

I want last 10 min records from database for that i have used this query
select * from x_table where x_time >=(now()-interval '10 minutes')
I'm accessing this remote server which is in different time zone from local machine and 11 hrs behind the my time
so when i'm trying the given query is not giving me correct output
what i can change in query so that i'll get the correct result.
If you know it's always going to be 11 hrs behind, just subtract another interval.
select *
from x_table
where x_time >=(now() -
interval '10 minutes' -
interval '11 hours')

Searching for variable between two times in SQL

I have a variable date_entered which is in DateTime format. I made a temp field TIME which is just the time portion of date_entered. Im trying to search for the amount of tickets entered for each hour between 7am and 7pm. I need to get tickets for each hour increment between 7am and 7pm so tickets between 7-8,8-9,9-10 etc.. and they are to be displayed in different columns.
Right now I have:
=Iif((Hour(Fields!Time.Value) >= 7) AND
(Hour(Fields!Time.Value) < 8), Fields!TicketNbr.Value, 0)
However this is not getting only tickets between this hour interval and instead all tickets for that day. How can I get tickets just in that hour period? I am also using BIDS through Microsoft Visual Studio. Thanks!
Why do you need a temp field TIME? There's a TIME datatype on SQL Server 2008 which allows you to do something like this:
SELECT * FROM YourTable WHERE CAST(YourDateField AS TIME) BETWEEN '07:00' and '19:00'

Dynamic Timestamp DB2 SQL

Using DB2 SQL
I would like to query for records since 2:00 yesterday. I want a dynamic expression that frees me from having to manually enter the current date prior to running the query. The created_datetime attribute is of timestamp dataype.
For example:
select record_key, other_stuff
from table
where created_datetime > "2 o'clock PM yesterday"
Is this kind of dynamic timestamp comparison even possible? Eventually, I'd like to be able to do a window of time, which gets complicated!
select count(1)
from table
where created_datetime between "2 o'clock PM yesterday" and "2 o'clock PM today"
I am familiar with current date, but I am trying to conceptualize how I would leverage that. The following gets me close, but it includes everything 24 hours prior to whenever the query is run.
select count(1)
from table
where created_datetime between (currentdate - 1 day) and (currentdate # 2 o'clock PM)
I know this is some pretty basic territory, and I feel guilty posting this question, but my research has not turned up anything for me so far. I appreciate every ounce of time spent on my behalf.
Try these
select record_key, other_stuff
from table
where created_datetime > CURRENT DATE - 10 HOURS
select count(1)
from table
where created_datetime between (CURRENT DATE - 10 HOURS) and (CURRENT DATE + 14 HOURS)
select count(1)
from table
where created_datetime between (CURRENT DATE - 1 DAYS) and (CURRENT DATE + 14 HOURS)
From the IBM Dev Works Library : DB2 Basics: Fun with Dates and Times
There are heaps of samples there.
E.g.
You can also perform date and time calculations using, for lack of a
better term, English:
current date + 1 YEAR
current date + 3 YEARS + 2 MONTHS + 15 DAYS
current time + 5 HOURS - 3 MINUTES + 10 SECONDS
Try this with this Timestamp option in you where clause.
Below sample to query for between last 24 hours.
select
timestamp(CURRENT date - 1 days,(CURRENT time - 24 hours)),
timestamp(CURRENT date,CURRENT time )
FROM
sysibm.sysdummy1;