This question already has answers here:
Truncate date to only hour / minute
(4 answers)
T-SQL datetime rounded to nearest minute and nearest hours with using functions
(4 answers)
Group DateTime into 5,15,30 and 60 minute intervals
(6 answers)
Closed 6 months ago.
I'm tring to extract hour values from a datetime:
I want all the datetimes like '2021-10-27 04:55:00.000' to show the hour data, so: '2021-10-27 04:00:00.000'
What query do i run to get this?
Thanks in advance!
Use date maths and a "magic" date:
DATEADD(HOUR,DATEDIFF(HOUR,0,YourColumn),0);
This gets the number of hours between the "date" 0 (1900-01-01) and your date value, and then adds that many hours to the "date" 0.
On SQL Server 2022 (currently in preview), however, you have access to DATETRUNC and DATE_BUCKET that make this much easier:
DATETRUNC(HOUR,YourColumn),
DATE_BUCKET(HOUR,0,YourColumn)
The current time (UK) is 11:06 (am) and:
select format(getdate(), 'yyyy-MM-dd HH')
..returns 2022-08-12 11
You can add whatever you want on the end of it, e.g.:
select format(getdate(), 'yyyy-MM-dd HH') + ':00:00'
..which gives 2022-08-12 11:00:00
NB Format is a SQL-Server function. I'm not sure how many other databases have it.
For the string representation of a date and time value you can do something like this
SELECT CONCAT(FORMAT(dt, 'yyyy-MM-dd HH'),'00:00.000')
see Custom formatting and Custom Date and Time Formatting
use convert
declare #a datetime='2021-10-27 04:55:00.000'
select convert(varchar(10),#a,120) + ' '+convert(varchar(2), datepart(hour,#a))+':00:00'
Related
This question already has answers here:
Get the records of last month in SQL
(2 answers)
Closed 12 months ago.
I have data in oracle table where date field Created_Date is in format 01/01/2022 7:00:00 PM which is of type varchar2 ,i want to get past one month data, and i did below query which is not working
select *from Mn_Fdd_tbl where to_date(to_char(Created_Date,'DD/MM/YYYY'), 'DD/MM/YYYY' ) > trunc(sysdate)-30;
Should be
select *
from mn_fdd_tbl
where to_date(created_date, 'dd/mm/yyyy hh:mi:ss pm') > add_months(trunc(sysdate), -1);
because
no point in TO_CHAR-ing something that's already a string ...
... with a wrong format model
"last month": not all months have 30 days, so - your "calculation" is wrong for approx. 50% of months. Use add_months instead
Trying to convert 43439.961377314816 into date. Currently I am using this code:
SELECT
(timestamp '1970-01-01 00:00:00 GMT' +
numtodsinterval(WRITETIMESTAMP, 'SECOND')) at time zone 'CST',
WRITETIMESTAMP
FROM
t.table
but I am getting this result:
01-JAN-70 06.03.59.961377315 AM CST
Date should be:
12/05/2018
This produces the date that you want:
select date '1899-12-30' + 43439.961377314816
from dual;
It looks like you are using Excel dates or something similar.
You have two problems in your query. First, you used the wrong base time. As pointed out by #GordonLinoff, the base time for an Excel date is actually 1900-01-01, and Excel treats 1900 as a leap year. This is not an error in Excel, per se, but a conscious design decision which was made to copy the (buggy) behavior of Lotus 1-2-3, which did have this bug. So - in Lotus 1-2-3 it's a bug, but in Excel it's a feature. :-) Secondly, in Excel dates the integer portion represents the number of days since the base date, and the fractional portion represent fraction of a day. In your NUMTODSINTERVAL call, however, you specified the interval_unit argument as 'SECOND'; it should have been 'DAY'.
Putting these things together we get
WITH cte AS (SELECT 43439.961377314816 AS WRITETIMESTAMP FROM DUAL)
SELECT
(timestamp '1899-12-30 00:00:00 GMT' + numtodsinterval(WRITETIMESTAMP, 'DAY')) at time zone 'CST',
WRITETIMESTAMP
FROM
cte
dbfiddle here
Best of luck.
This looks like expected behavior to me. 43439 seconds/60/60 = 12 hours and you're getting about 12 hours from the starting timestamp.
SELECT numtodsinterval('43439.961377314816', 'SECOND') as i FROM dual;
I
----------------------
+00 12:03:59.961377315
Why would you think that would give you a date in 2018?
Here is a working formula to put in Excel that works for Chromium browsers.
Chrome/Edge: =((Cell/1000000-11644473600)*1000000)/86400000000+DATE(1970,1,1)
This question already has answers here:
A way to extract from a DateTime value data without seconds
(9 answers)
Closed 4 years ago.
I have a column in my table (having value- 2017-09-27 15:39:36.000).
I want to select only Date, Hour, Minutes (i.e:- 2017-09-27 15:39) from column.
Can anyone please tell me how to select.?
Use the FORMAT function to format the date:
SELECT FORMAT(CAST('2017-09-27 15:39:36.000' AS DATETIME), 'yyyy-MM-dd HH:mm')
-- 2017-09-27 15:39
Here is a list of available format specifiers.
One option would be to use CONVERT with a mask matching the format you want:
SELECT CONVERT(varchar(16), GETDATE(), 120)
2018-09-28 07:58
Demo
We convert to varchar(16) explicitly so that only the hour and minute components are retained.
Use the datepart function which can extract specific parts of the date value.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
I came across a SQL query with below conditional clause
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS') between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) and :endDtTime
My high level understanding is that create time stamp should be between some time before end time and end time.
Not sure what does TO_DATE('235959', 'HH24MISS') mean.
If I run the below query on 5th Feb it returns 1st Feb
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl
Please help me understand what exactly this condition mean.
TO_DATE('235959', 'HH24MISS') creates a DATE value. Note, in Oracle data type DATE always contains date and time part.
If you don't provide any date value then Oracle defaults it to the first day of current months, so TO_DATE('235959', 'HH24MISS') returns "2018-02-01 23:59:59"
I don't think this condition makes sense:
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS')
between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and :endDtTime
First, you should compare DATE values, not strings.
I assume TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) is wrong. I think you mean TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60)
This will subtract 1 day plus 1 Second (1/24/60/60), i.e. subtract 23:59:59.
Another possibility would be TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - INTERVAL '23:59:59' HOUR TO SECOND.
So, your condition could be
WHERE CRTE_TMS between TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60) AND :endDtTime
This could probably be a comment instead of an answer.. Sorry do not have enough reputation.
HH24 is the 24 hour format of the hours.
235959 is 23 hours 59 minutes 59 second.
In a 12 hour format it means 11:59:59 PM.
The thing you are trying to do is converting date format into character and comparing it with other dates by converting them to character format using To_char. I do not suggest that.
The below would give the first of the month
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl;
I am not able to understand what you are trying to achieve here.
The below syntax gives in the character format which is the difference between two dates. for example 4 days and 10 hours.
To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and then you are trying to do a comparision like date between (4 days and 10 hours) and :endtime. This is incorrect.
You could use the below to convert to date format.
to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS')
select case when to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS') between :begindate and :enddate then 1
else null
from dual;
This question already has an answer here:
Formatting DATE in oracle
(1 answer)
Closed 7 years ago.
Need to acquire YYYY-MM of SYSDATE and load to column (Data_Type = Date). The below query gives the required result, however, being convert to string datatype in the process.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM') FROM DUAL;
Any way to retain the Date datatype & acquire SYSDATE as YYYY-MM by purely using oracle
Dates don't have formats, so you can't set the format when it is saved. Formats only come into play when converting to or from a string.
If you want to save just the month year with day and time being 1 and midnight, trunc(date_value, 'month'). Fomat models for round and trunc date.
Convert it back to a date.
SELECT to_date(TO_CHAR(SYSDATE, 'YYYY-MM'),'YYYY-MM') FROM DUAL
Of course, to be a valid date there must be a day component. This will be set to 1.
Alternatively, why not just store the sysdate? Next time you look at that value you will only be interested in the year and month.