Drupal: getting exact date from created column in ddbb - sql

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;

Related

SQL timestamp filtering based only on time

I want to create a query in Oracle SQL that will grab records from a given time interval, during certain hours of the day, e.g. records between 10am to noon, in the past 10 days. I tried this, but it does not work:
select * from my_table where timestamp between
to_timestamp('2020-12-30','YYYY-MM-DD')
and
to_timestamp('2021-01-08','YYYY-MM-DD') and
timestamp between
to_timestamp('10:00:00','HH24:MI:SS')
and
to_timestamp('12:00:00','HH24:MI:SS')
where timestamp is of type TIMESTAMP. I have also thought of using a join, but I am struggling to find a way to filter on time of day.
Is there a way to filter using only the time, not the date, or a way to filter on time for every day in the interval?
select *
from my_table
where timestamp between to_timestamp('2020-12-30','YYYY-MM-DD')
and to_timestamp('2021-01-08','YYYY-MM-DD')
and timestamp - trunc(timestamp) between interval '10' hour
and interval '12' hour
If you don't need to include exactly noon (including no fractional seconds), you could also do
select *
from my_table
where timestamp between to_timestamp('2020-12-30','YYYY-MM-DD')
and to_timestamp('2021-01-08','YYYY-MM-DD')
and extract( hour from timestamp ) between 10 and 11
As an aside, I'd hope that your actual column name isn't timestamp. It's legal as a column name but it is a reserved word so you're generally much better off using a different name.

What is the difference between these 2 sql queries?

select * from accounts where sysdate - 100 < last_active_dt;
select * from accounts where sysdate - 100 <= last_active_dt;
Both of the queries returned the same result.
I was thinking that for the first query, it would not select
records on the day of last_active_dt. For the 2nd query, it
will select records on the day of last_active_dt.
But this does not seem to be the case as both returned the same results.
Is there any difference to it? And how can I see the difference?
last_active_dt column follows a date format DD-MON-YYYY
last_active_dt column follows a date format DD-MON-YYYY
No, it doesn't (but it may look like it does in the user interface you are using).
A DATE data type always has year, month, day, hour, minute and second components and is stored (without any formatting) in 7 bytes. It is the user interface that applies a (default) format to the date and this may hide the time component from you.
You can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
To change the default date format used in SQL*Plus (and SQL Developer) to see the date with all the components.
Equally, SYSDATE is of a DATE data type and has year, month, day, hour, minute and second components so what you are probably intending is:
select * from accounts where TRUNC( sysdate ) - INTERVAL '100' DAY < last_active_dt;
This will get all the columns where the last_active_dt is after midnight of the day 100 days ago (even if it 1 second after). This will work if all your last_active_dt values have the times TRUNCated to exactly midnight 00:00:00 but not if the column as non-zero time components as you will still get values with times from 00:00:01 to 23:59:59 of the day 100 days ago.
If your last_active_dt has a non-zero time component then you want:
select * from accounts where TRUNC( sysdate ) - INTERVAL '99' DAY <= last_active_dt;

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'

Server time is not aligning with local time

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."'
)