hello my friends I got tired of looking for a solution I have a postgresql database that has a timestamp column I want to extract the values this way
2010-01-01 14:34:43
without the milliseconds how to do this I want it
2010-01-01 14:34:43
and he shows me like this
2010-01-01 14:34:43.267543
If you want to discard the milliseconds you can use date_trunc():
date_trunc('second', mytimestamp)
On the other hand if you want to round to the closest second, you can cast to timestamp(0):
mytimestamp::timestamp(0)
Demo on DB Fiddle - I used a timestamp whose tens of seconds is greater than 5 to make the test representative:
select
mytimestamp,
date_trunc('second', mytimestamp) trunc_mytimestamp,
mytimestamp::timestamp(0) round_mytimestamp
from (values('2010-01-01 14:34:43.567543'::timestamp)) as t(mytimestamp)
mytimestamp | trunc_mytimestamp | round_mytimestamp
:------------------------- | :------------------ | :------------------
2010-01-01 14:34:43.567543 | 2010-01-01 14:34:43 | 2010-01-01 14:34:44
Related
I have some tables in a postgres that have a column for year and a column for quarter (both stored as bigint). I need to be able to combine those together in the output of a query in the form of 'YYYYQ' (not the hard part) AND have the datatype of that field be datetime (<--the hard part).
The only query I have attempted that didn't fail was -
SELECT to_date((year::VARCHAR + quarter::VARCHAR),'YYYYQ') AS Stuff
FROM company.products
And while the output is in DATETIME format, there is no Quarter info in it.
Sample -
stuff
2011-01-01
2011-01-01
2012-01-01
2012-01-01
2012-01-01
Is it even possible to create output that has the format 'YYYYQ' AND is in DATETIME format? And if so, how?
From the PostgreSQL docs (emphasis mine):
In to_timestamp and to_date, weekday names or numbers (DAY, D, and related field types) are accepted but are ignored for purposes of computing the result. The same is true for quarter (Q) fields.
You can save the date of the 1st day of the quarter. Multiply the recorded quarter -1 by 3.
SELECT to_date('2021','YYYY') + interval '6 month';
?column?
---------------------
2021-07-01 00:00:00
SELECT to_char(to_date('2021','YYYY') + interval '6 month','YYYYQ');
to_char
---------
20213
SELECT q,
to_char(to_date('2021','YYYY') + interval '3 month'*(q-1),'YYYYQ') as YYYYQ,
to_date('2021','YYYY') + interval '3 month'*(q-1) as d
FROM generate_series(1,4) f(q);
q | yyyyq | d
---+-------+---------------------
1 | 20211 | 2021-01-01 00:00:00
2 | 20212 | 2021-04-01 00:00:00
3 | 20213 | 2021-07-01 00:00:00
4 | 20214 | 2021-10-01 00:00:00
Don't ask me why but for some reason we have a date time column that is in the wrong format that I need help converting.
Example timestamp from DB: 01-OCT-20 12.18.44.000000000 AM
In the example above the hours is actually 18 and the minutes is 44.
Not sure how this happened by 12 is the default for everything. All I want to do is get the difference in HH:MM from 2 timestamps, but i dont know how to convert this properly with the hours being in the minute section and the minutes being in the seconds section.
Example of what I'm looking for:
01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM
Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.
Thanks,
In the example above the hours is actually 18 and the minutes is 44.
Not sure how this happened by 12 is the default for everything. All I want to do is get the difference in HH:MM from 2 timestamps, but i dont know how to convert this properly with the hours being in the minute section and the minutes being in the seconds section.
To convert minutes to hours, you need to multiply by 60.
To convert seconds to minutes, you also need to multiply by 60.
So, if you want to convert the time part of the correct value then you take the time since midnight and multiply it all by 60.
If you want to get the difference between the current and correct time (after multiplying by 60) then you want to subtract the current time (which can be simplified to just multiplying by 59).
So to get the time difference you can use:
SELECT (value - TRUNC(value))*59 AS difference,
value + (value - TRUNC(value))*59 AS updated_value
FROM table_name;
So, for your sample data:
CREATE TABLE table_name ( value ) AS
SELECT TO_TIMESTAMP( '01-OCT-20 12.18.44.000000000 AM', 'DD-MON-RR HH12.MI.SS.FF9 AM' ) FROM DUAL
Then the output is:
DIFFERENCE | UPDATED_VALUE
:---------------------------- | :-------------------------
+000000000 18:25:16.000000000 | 2020-10-01 18:44:00.000000
db<>fiddle here
If you want to compare two wrong values just subtract one timestamp from the other and multiply by 60 (assuming that the hour will always be 12 AM or 00 in the 24 hour clock):
SELECT (value1 - value2) * 60 AS difference,
value1,
value1 + (value1 - TRUNC(value1))*59 AS updated_value1,
value2,
value2 + (value2 - TRUNC(value2))*59 AS updated_value2
FROM table_name;
So, for the sample data:
CREATE TABLE table_name ( value1, value2 ) AS
SELECT TO_TIMESTAMP( '01-OCT-20 12.18.44.000000000 AM', 'DD-MON-RR HH12.MI.SS.FF9 AM' ),
TO_TIMESTAMP( '01-OCT-20 12.12.42.000000000 AM', 'DD-MON-RR HH12.MI.SS.FF9 AM' )
FROM DUAL
The output is:
DIFFERENCE | VALUE1 | UPDATED_VALUE1 | VALUE2 | UPDATED_VALUE2
:---------------------------- | :------------------------- | :------------------------- | :------------------------- | :-------------------------
+000000000 06:02:00.000000000 | 2020-10-01 00:18:44.000000 | 2020-10-01 18:44:00.000000 | 2020-10-01 00:12:42.000000 | 2020-10-01 12:42:00.000000
Which gives the difference as 6 hours and 2 minutes.
db<>fiddle here
I have a set of data where one column is date and time. I have been asked for all the data in the table, between two date ranges and within those dates, only certain time scale. For example, I was data between 01/02/2019 - 10/02/2019 and within the times 12:00 AM to 07:00 AM. (My real date ranges are over a number of months, just using these dates as an example)
I can cast the date and time into two different columns to separate them out as shown below:
select
name
,dateandtimetest
,cast(dateandtimetest as date) as JustDate
,cast(dateandtimetest as time) as JustTime
INTO #Test01
from [dbo].[TestTable]
I put this into a test table so that I could see if I could use a between function on the JustTime column, because I know I can do the between on the dates no problem. My idea was to get them done in two separate tables and perform an inner join to get the results I need
from #Test01
WHERE justtime between '00:00' and '05:00'
The above code will not give me the data I need. I have been racking my brain for this so any help would be much appreciated!
The test table I am using to try and get the correct code is shown below:
|Name | DateAndTimeTest
-----------------------------------------|
|Lauren | 2019-02-01 04:14:00 |
|Paul | 2019-02-02 08:20:00 |
|Bill | 2019-02-03 12:00:00 |
|Graham | 2019-02-05 16:15:00 |
|Amy | 2019-02-06 02:43:00 |
|Jordan | 2019-02-06 03:00:00 |
|Sid | 2019-02-07 15:45:00 |
|Wes | 2019-02-18 01:11:00 |
|Adam | 2019-02-11 11:11:00 |
|Rhodesy | 2019-02-11 15:16:00 |
I have now tried and got the data to show me information between the times on one date using the below code, but now I would need to make this piece of code run for every date over a 3 month period
select *
from dbo.TestTable
where DateAndTimeTest between '2019-02-11 00:00:00' and '2019-02-11 08:30:00'
You can use SQL similar to following:
select *
from dbo.TestTable
where (CAST(DateAndTimeTest as date) between '2019-02-11' AND '2019-02-11') AND
(CAST(DateAndTimeTest as time) between '00:00:00' and '08:30:00')
Above query will return all records where DateAndTimeTest value in date range 2019-02-11 to 2019-02-11 and with time between 12AM to 8:30AM.
I have a table that looks like this:
id | flight_number | departure_time | arrival_time
---+---------------+----------------+-------------
1 | UAL123 | 07:00:00 | 08:30:00
---+---------------+----------------+-------------
2 | AAL456 | 07:30:00 | 08:40:00
---+---------------+----------------+-------------
3 | SWA789 | 07:45:00 | 09:10:00
I'm trying to figure out an SQL query that can get upcoming flights based on departure time given the current time. For instance, at 07:20, I would like to return AAL456, SWA789 since those flights have not departed yet. At 07:40, I would like to just return SWA789. What is a good way to do this?
Well, you can use LOCALTIME to get the current time. So, if the departure_time is stored as a time, then:
select t.*
from t
where t.departure_time > localtime;
This assumes no time zone information is part of the time value. Also, it will return no flights after the last flight has departed for a day (which is consistent with the phrasing of your question).
I have a table containing a datetime column and some misc other columns. The datetime column represents an event happening. It can either contains a time (event happened at that time) or NULL (event didn't happen)
I now want to count the number of records happening in specific intervals (15 minutes), but do not know how to do that.
example:
id | time | foreign_key
1 | 2012-01-01 00:00:01 | 2
2 | 2012-01-01 00:02:01 | 4
3 | 2012-01-01 00:16:00 | 1
4 | 2012-01-01 00:17:00 | 9
5 | 2012-01-01 00:31:00 | 6
I now want to create a query that creates a result set similar to:
interval | COUNT(id)
2012-01-01 00:00:00 | 2
2012-01-01 00:15:00 | 2
2012-01-01 00:30:00 | 1
Is this possible in SQL or can anyone advise what other tools I could use? (e.g. exporting the data to a spreadsheet program would not be a problem)
Give this a try:
select datetime((strftime('%s', time) / 900) * 900, 'unixepoch') interval,
count(*) cnt
from t
group by interval
order by interval
Check the fiddle here.
I have limited SQLite background (and no practice instance), but I'd try grabbing the minutes using
strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
with the %M modifier (http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html)
Then divide that by 15 and get the FLOOR of your quotient to figure out which quarter-hour you're in (e.g., 0, 1, 2, or 3)
cast(x as int)
Getting the floor value of a number in SQLite?
Strung together it might look something like:
Select cast( (strftime( 'YYYY-MM-DD HH:MI:SS', your_time_field, '%M') / 15) as int) from your_table
(you might need to cast before you divide by 15 as well, since strftime probably returns a string)
Then group by the quarter-hour.
Sorry I don't have exact syntax for you, but that approach should enable you to get the functional groupings, after which you can massage the output to make it look how you want.