I am trying to run a T-SQL query that would return all rows that contain a timestamp between 00:00:00 and now for any given date.
I've used the following code, but this only returns items within the past 24 hours:
SELECT *
FROM table
WHERE timestamp_closed = DATE(GETDATE()-1);
Here you have the number of rows:
SELECT COUNT(*)
FROM *yourtable*
WHERE timestamp_closed BETWEEN CAST(GETDATE() AS DATE) AND GETDATE()
SELECT *
FROM table
WHERE timestamp_closed BETWEEN CAST(GETDATE() AS DATE) AND GETDATE()
You could build the datevalue for "Today at 00:00:00" and now and then do a
WHERE timestamp_closed >= "Today at 00:00:00" and timestamp_closed<=GETDATE()
You can probably wrap this in a function.
select *
from table
where datepart(hh,timestamp_closed)*100 + datepart(mi,timestampclosed) <
datepart(hh,getdate())*100 + datepart(mi,getdate())
SELECT *
FROM table
WHERE (timestamp_closed > CAST(#specificDate AS DATE)
AND timestamp_closed <= GETDATE())
Related
I want to obtain the last entries in the last 30 seconds from a MSSQL database.
In Mysql you do:
SELECT * FROM table WHERE datetime > (now() - interval 30 second)
How to do that in MSSQL?
DATEDIFF is your solution here.
SELECT
*
FROM
[table]
WHERE
DATEDIFF(second, [datetime], GETDATE()) < 30
Using dateadd with a negative integer will give you the data you are asking for
DATEADD (Transact-SQL)
SELECT
*
FROM
table
WHERE
datetime >= DATEADD(SECOND,-30,datetime)
You can use GETDATE() in combindation with DATEADD:
SELECT * FROM table WHERE datetime > (DATEADD(second,-30,getdate()))
Use DATEADD()
DATEADD(SECOND,-30,GETDATE())
Hey how would I get data for a date column that's older than 6 months?
select * from myTable where dateColumn >
Thanks
Use DATEDIFF function.
Read more here: https://msdn.microsoft.com/en-us/library/ms189794.aspx
SELECT *
FROM myTable
WHERE DATEDIFF(MM, dateColumn, GETDATE()) > 6
SELECT * FROM myTable WHERE DATEDIFF(day, NOW(), dateColumn) > 180
select *
from table
where
date_column >=
DATEADD(m, -6, convert(date, convert(varchar(6), getdate(),112) + '01'))
I would use
select * from myTable where dateColumn > DATEADD(mm,GETDATE(),-6)
This way you arent applying a function on the lookup column, which in some cases can result in performance issues
Dateadd is very simple to use.
the first parameter is the interval, m means month, d means day ect. the second parameter is the increment, and the last one is obviously the date
select dateadd(m,6,getdate())
more info here
http://www.w3schools.com/sql/func_dateadd.asp
Once you understand dateadd then you can simply use it in where clause as such
Select * from Table1 where date1 >= dateadd(m,6,date1)
I have a datetime column - createddatetime with this value 2015-02-10 23:54:02.000
All I have to do is to compare ONLY THE DATE with getdate() function (Time can be ignored)
How should the query be written.
the pseudo query is:
select * from table where createddatetime(only date) = getdate(only date)
USE CAST or CONVERT function:
select * from table where cast(createddatetime as date) = cast(getdate() as date)
Lots of ways. My favorite is with DATEDIFF()
SELECT * FROM Table WHERE DATEDIFF(day,createddatetime,getdate()) = 0
I have 2 columns in a table Start date and EndDate , i want to enter Current date and it should compare current date with start and end date if current date exist between start date or enddate must return that row. i have query but not good result found
select * from Seasons where CONVERT (date, GETDATE()) between '#startdate' and '#Endate'
Try this:
SELECT * FROM seasons
WHERE getdate() >= #startdate
AND getdate() <= #enddate
SELECT *
FROM mytable
WHERE SYSDATE() BETWEEN start_date AND end_date
Please check whether the below code works for you.
if both the date (startdate and enddate) should match the current date then use this.
select * from Seasons where convert(date,startdate) = CONVERT (date, GETDATE()) and convert(date,Endate) = CONVERT (date, GETDATE())
if any one of the date (startdate or enddate) should match the current date then use this.
select * from Seasons where convert(date,startdate) = CONVERT (date, GETDATE()) or convert(date,Endate) = CONVERT (date, GETDATE())
I'm looking to update the SOLUTION_ID where the CREATED_DATE is Greater Than or Equal To the given date.
What I have so far is updating all the records in the Table. CREATED_DATE has a smalldatetime datatype.
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE CAST(CREATED_DATE AS datetime) >= 2011-05-08
Help greatly appreciated!
You want to quote the date;
WHERE CREATED_DATE >= '2011-05-08'
(unquoted 2011-05-08 is treated as a mathematical expression that evaluates to 1998)
Casting of column with smalldatetime is not required. Please see below the sample
create table #temp
(
dat smalldatetime,
Solution_ID int
)
insert into #temp(dat, Solution_ID)values(1, GETDATE())
insert into #temp(dat, Solution_ID)values(2, GETDATE()+1)
insert into #temp(dat, Solution_ID)values(3, GETDATE()+2)
select * from #temp
where dat >= 'Your date'
Update #temp
Set Solution_ID = 4
Where dat >= 'Your date'
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE CAST(CREATED_DATE AS datetime) >= '2011-05-08'
No need for the cast at all.
And your query won't work exactly as you expect in that 2011 - 5 - 8 = 1998 which in sql server is 22/6/1905...
>= '2011-05-08'
Use the DATEDIFF function
DATEDIFF(dd, #GIVEN_DATE, CREATED_DATE) >= 0
This will return the number of days difference from your given date and your created date. When the function returns zero or greater, then the created_date is greater than the given date. No need for casting.
UPDATE [Database].[dbo].[TB__TABLE]
SET SOLUTION_ID = 1
WHERE DATEDIFF(dd, #GIVEN_DATE, CREATED_DATE) >= 0