I have a table:
ID Timestamp
1 2010-07-27 13:14:00.000
2 2010-08-13 13:14:00.000
3 2010-12-21 13:14:00.000
Now I need to subtract the day from Timestamp column with current getdate() and get the days from it.
You can use DATEDIFF
SELECT DATEDIFF(day, Timestamp, getdate())
FROM YourTable
DATEDIFF
Returns the count (signed integer) of the specified datepart
boundaries crossed between the specified startdate and enddate.
Syntax
DATEDIFF ( datepart , startdate , enddate )
You can use datediff to calculate the difference between datetime values.
declare #T table
(
ID int,
Timestamp datetime
)
insert into #T values
(1, '2010-07-27 13:14:00.000'),
(2, '2010-08-13 13:14:00.000'),
(3, '2010-12-21 13:14:00.000')
select datediff(day, Timestamp, getdate())
from #T
you can use.
CURRENT_TIMESTAMP
Related
In SAP B1, I wanted to count data that is created 3 hours prior to current time.
I already have this logic
SELECT COUNT(*)
FROM OIVL T0
WHERE T0.CreateDate >= DATEADD(hour, -12, GETDATE())
But the problem is the CreateDate column only contains Date, without time.
It has time but in smallint
Given the discussion in comments about how CreateTime is encoded by SAP B1, you're going to need to compute each row's datetime value to compare it with the current time, e.g.:
create table #Example (
CreateDate date,
CreateTime smallint
);
insert #Example (CreateDate, CreateTime) values ('2020-08-30', 2324);
select
CreateDate,
CreateTime,
CreateDateTime --2020-08-30 23:24:00.000
from #Example
outer apply (
select dateadd(mi, (CreateTime/100)*60+CreateTime%100, cast(CreateDate as datetime)) as CreateDateTime
) Calc
where CreateDateTime >= dateadd(hour, -12, getdate());
What will be the best script to get the records from a certain table with column createdDate and modifiedDate where modifiedDate is the only nullable column and is equal to date now/current date?
In case that there will be null values from modified date column, the script will get the created date.
The output should get all records with latest created or modified data. I tried these script below:
SELECT *
FROM table
WHERE ISNULL(CONVERT(date, ModifiedDate),
CONVERT(date,CreatedDate)) = CONVERT(date, getdate())
or
SELECT *
FROM table
WHERE CASE WHEN ModifiedDate IS NULL
THEN CONVERT(date, CreatedDate)
ELSE CONVERT(date,ModifiedDate) END = CONVERT(date, getdate())
If you just need this against today (= GETDATE()) this should be simple as this:
SELECT *
FROM tbl
WHERE CAST(CreatedDate AS DATE) = CAST(GETDATE() AS DATE)
OR (ModifiedDate IS NOT NULL AND CAST(ModifiedDate AS DATE)= CAST(GETDATE() AS DATE));
Some thoughts: It is very bad to use functions (here: ISNULL()) in predicats. The engine will not be able to apply existing indexes. Read about sargable. One exception is CAST(SomeDateTime AS DATE) which is sargable.
If you do not need this for any date (just for today), it should be enough to say: This row was created today or it was modified today.
Important Be sure that there are indexes for ModifiedDate and CreatedDate!
Is this what you are looking for?
SELECT
someColumn1,
someColumn2,
CASE
WHEN modifiedDate IS NULL THEN createdDate
ELSE modifiedDate
END AS someDate
FROM someTable
You may use ISNULL() function in TSQL
SELECT *
FROM table
WHERE
ISNULL(CONVERT(date,ModifiedDate), CONVERT(date, CreatedDate)) = CONVERT(date, getdate())
If using function will affect the indexes, kindly try this one if this is efficient.
SELECT *
FROM table
WHERE ModifiedDate IS NOT NULL
AND CONVERT(date,ModifiedDate) = CONVERT(date, getdate()
UNION
SELECT *
FROM table
WHERE ModifiedDate IS NULL
AND CONVERT(date,CreatedDate) = CONVERT(date, getdate()
I am trying to get order counts for everyday this year in 3pm to 3pm intervals
Select cast(order_datetime as date) ,count(*)
from order_table
where order_datetime> '01/01/2015 15:00'
group by ?????
I normally group by
cast(order_datetime as date) ---for 12am to 12am
but I want 3pm to 3pm.
This might be solution looking for i have taken a sample data and a table variable #t to solve it col1 in the table refers to Your problem
SET DATEFORMAT DMY
DECLARE #t TABLE
(
Id int,
Name varchar(11),
[Col1] DateTime
)
INSERT INTO #t
VALUES(1,'ABC','22/12/2012 03:45:00 PM'),(2,'SD','22/12/2012 03:01:00 PM'),(3,'SDSA','22/12/2012 02:01:00 PM'),(4,'ASDF','22/12/2012 03:30:00 PM'),(5,'ASWER','22/12/2012 02:30:00 PM')
,(11,'NARI','21/12/2012 03:40:00 PM')
SELECT CASE WHEN CAST(COL1 AS TIME) >'15:00:00' THEN CAST(DATEADD(DAY,1,Col1) AS date) ELSE CAST(Col1 AS DATE) END AS ORDERS,COUNT(*) AS COUNT
FROM #t
GROUP BY CASE WHEN CAST(COL1 AS TIME) >'15:00:00' THEN CAST(DATEADD(DAY,1,Col1) AS date) ELSE CAST(Col1 AS DATE) END
I have a column of dates that range from 2015-06 to 2013-04. The date range will grow as I put in more data. How do I write a query where the date is always a X amount of months before the current date?
date
2015-06
2015-05
2015-04
2015-03
2015-02
for example I want it to be:
Select *
From dbo.name
Where date in (X months ago from current date)
if todays date is 2015-12 and I want 3 months ago, I want the query to be:
Select *
From dbo.name
Where date in ('2015-11','2015-10','2015-09')
Thanks
Select *
From dbo.name
Where CAST([Date] + '-01' AS DATE) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -3, 0)
Since your date strings are in yyyy-mm format you can do string comparison using < > =. You just need to convert the current date to varchar(7) using style 126 (yyyy-mm-ddThh:mi:ss.mmm)
For Example
DECLARE #CurrentDate DATETIME = GETDATE()
DECLARE #XMonthsAgo INT = 3
DECLARE #Dates TABLE ([date] varchar(7))
INSERT INTO #Dates VALUES
('2015-09'),('2015-08'),('2015-07'),('2015-06'),('2015-05'),('2015-04'),('2015-03'),
('2015-02'),('2015-01'),('2014-12'),('2014-11'),('2014-10'),('2014-09'),('2014-08'),
('2014-07'),('2014-06'),('2014-05'),('2014-04'),('2014-03'),('2014-02'),('2014-01')
SELECT *
FROM #Dates d
WHERE [date] >= CONVERT(VARCHAR(7), DATEADD(month, -#XMonthsAgo, #CurrentDate), 126)
AND [date] < CONVERT(VARCHAR(7), #CurrentDate, 126)
Result
date
----
2015-07
2015-06
2015-05
I have a table with two fields - datetime and int. I want to do a group by on the datetime only on the date ignoring the hour and minute. The SELECT statement should return a date that maps to the sum of the int of a single day.
SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumField
FROM MyTable
GROUP BY CAST(Datetimefield AS DATE)
As he didn't specify which version of SQL server he uses (date type isn't available in 2005), one could also use
SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summed
FROM table_name
GROUP BY CONVERT(VARCHAR(10),date_column,112)
I came researching the options that I would have to do this, however, I believe the method I use is the simplest:
SELECT COUNT(*),
DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup
FROM TABLE
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0)
ORDER BY dtgroup ASC;
-- I like this as the data type and the format remains consistent with a date time data type
;with cte as(
select
cast(utcdate as date) UtcDay, DATEPART(hour, utcdate) UtcHour, count(*) as Counts
from dbo.mytable cd
where utcdate between '2014-01-14' and '2014-01-15'
group by
cast(utcdate as date), DATEPART(hour, utcdate)
)
select dateadd(hour, utchour, cast(utcday as datetime)) as UTCDateHour, Counts
from cte
Personally i prefer the format function, allows you to simply change the date part very easily.
declare #format varchar(100) = 'yyyy/MM/dd'
select
format(the_date,#format),
sum(myfield)
from mytable
group by format(the_date,#format)
order by format(the_date,#format) desc;