SQL: Query where date is like today - sql-server

I have a little problem with my SQL Query:
I have this value on my table: 2014-10-23 00:00:00
I have a record like this each 2 minutes every day, and I need to SELECT all the value of today.
Now I made this:
WHERE mytable.data LIKE CURDATE()
and it doesn't work. I tried a lot of things found here on stackoverflow and nothing could help me.
Thanks for answer.

You don't say which version of SQL Server you are using, if you have the DATE data type available you can cast the datetime returned by getdate() or CURRENT_TIMESTAMP to strip off the time part.
But then you need a range to find all the matching rows, something like:
SELECT *
FROM mytable
WHERE mytable.data >= CAST(GETDATE() AS date)
AND mytable.data < CAST(DATEADD(day, 1, GETDATE()) AS date)
SQL Fiddle

Related

Epoch timestamp converted to datetime but how to sort records later on

I got my answer from Martin on how to convert epoch to date time but now I am facing another issue. How to do I filter records that are only entered today or some other date?
I created view with DATEADD(SECOND, Timing, '19700101') and want to filter out records.
select * from [dbo].[vw_records]
where Timing like '%23-10-2019%'
This way I am not able to query datetime based on that view. So as far the conversion it is good solution but for querying records not too good.
On other hand, maybe it is better to convert it in the table and then query results. hm...
Any suggestions please..
If you want rows from today, then use inclusive Date logic:
SELECT {Columns list}
FROM YourTable
WHERE DateColumn >= CONVERT(date, GETDATE())
AND DATECOLUMN < DATEADD(DAY, 1, CONVERT(date, GETDATE()));
For today, this'll return all rows on or after 2019-10-28T00:00:00:00.000 and before (but not including) 2019-10-29T00:00:00:00.000.

Converting String to Int in SQL Server

I have column Terms in my table and it contains data like this:
30D, 40D, etc.
D represents days.
My question: how can I sum date in Terms column? How can I convert string to int?
Thank you in advance.
Just use REPLACE to ditch the D and CONVERT to convert the varchar to a number....
SELECT SUM(CONVERT(int, REPLACE(Terms,'D',''))) FROM TableName
Edit: Commentor is right, CAST would work too.
And I dont get all the down votes. The guy's just asking a SQL question.
Jeez... Tough crowd.
Edit2:
OK, based on comments, it seems like you would like to get a "due date" from the terms (say, TODAY + 30D or "today + 30 days"). To do that, we'd need a DATE column. OR, we can just use today's date (GETDATE())
Assume your table has a date column called ... dt
The SQL to pull dt+'30D' would require us to add 30 "days" to dt.
DATEADD will add days, and the aforementioned CONVERT+REPLACE combo will convert '30D' to just plain '30' ...
So, you end up with the following SQL:
SELECT DATEADD(day, CONVERT(int, REPLACE(Terms,'D','')), dt) FROM TableName
The 'day' tells DATEADD to add days (that seems really obvious ... now),
the CONVERT+REPLACE tells it how many days to add
AND - dt is our column name.
SO - how about just adding "30D" to TODAY? Easy. We just swap out dt with GETDATE() ...
SELECT DATEADD(day, CONVERT(int, REPLACE(Terms,'D','')), GETDATE()) FROM TableName
SELECT AVG(SALARY) - AVG(CONVERT(int, REPLACE(SALARY,'0','')))
FROM EMPLOYEES;

How do I add to the date in a select query in SQL Server?

Apologies for the unclear question I was unsure of how to title this. I am new to SQL Server. I have two columns in my table, one called datetimeRented and one called datetimeReturned. I want to have a select query which calculates movies that are overdue (+48hrs) so that it will be something along the lines of:
select *
from ----
[where datetimeReturned is datetimeRented + 2 DAYS] --- something which essentially does something like this.
I just want to know what I would need to do to work this out, thank you :)
Use DATEADD (Transact-SQL)
For example:
WHERE datetimeReturned > DATEADD(day, 2, datetimeRented)
DATEADD function is useful for this tasks.
DATEADD (dd, 2, GETDATE()) will add days (dd), 2, to the given date (in this case the current date)
So your where clause could be like this
where datetimeReturned > DATEADD(dd, 2, datetimeRented)

Only filter my time in SQL Server where clause

I am new to SQL and Programming in general. I have a datetime field which I want to use to filter my results but only on the time portion.
For example I need to run a report from 2pm yesterday to 7am current day. I cannot hard code the dates because this report needs to run daily. This Query will run from a stored procedure automatically.
I have tried AND clm.createDtTm > DATEADD(d, -1, GETDATE()), which goes back one day but not the time range I need.
I tried this: AND (datepart(hh, '11:02:54.107') = 7)<--To be honest not even sure what I am doing here.
I am not sure if this is even possible, but if I can get results for one day back I am assuming there has to be a way to narrow that day between hours.
Any help would be appreciated.
Thanks,
Abdul
This should do the trick, it filters out rows that do not fall in the date interval, from 7pm yesterday to 7am today.
WHERE
createDtTm >= DATEADD(HOUR, 14,CAST(DATEADD(DAY,-1, CAST(GETDATE() AS DATE)) AS DATETIME))
AND createDtTm <= DATEADD(HOUR, 7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
I suggest something like this:
SELECT(DATEADD(d,-1,GETDATE()))
FROM table
WHERE (DATEADD(d,-1,GETDATE())) = rowloadtimestamp
HAVING DATEPART(HH,rowloadtimestamp) BETWEEN 13 AND 24
UNION
SELECT GETDATE()
FROM table
WHERE GETDATE() = rowloadtimestamp
HAVING DATEPART(HH,GETDATE()) BETWEEN 1 AND 7
Assuming you have a rowloadtimestamp field this should work for you.
What do you mean with „I can not hardcode the dates” ? Do you mean the time interval is variable, or is it fixed, or are you referring to the exact dates? I'm not sure I understand.
In case of MySQL you can do something like:
SELECT data, date FROM table WHERE date < DATE_SUB(NOW(), INTERVAL 2 DAY);
This Query will get you the last two days. You can vary the interval by replacing the „2” with a variable if that is possible in your case.

How to add days to the current date?

I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value.
eg: Current Date is 11/04/2014
And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the same date 11/04/2014. the year is not changing.
Here is my code:
select dateadd(dd,360,getdate())
Just do-
Select (Getdate()+360) As MyDate
There is no need to use dateadd function for adding or subtracting days from a given date. For adding years, months, hours you need the dateadd function.
select dateadd(dd,360,getdate()) will give you correct date as shown below:
2017-09-30 15:40:37.260
I just ran the query and checked:
Dateadd(datepart,number,date)
You should use it like this:
select DATEADD(day,360,getdate())
Then you will find the same date but different year.
From the SQL Server 2017 official documentation:
SELECT DATEADD(day, 360, GETDATE());
If you would like to remove the time part of the GETDATE function, you can do:
SELECT DATEADD(day, 360, CAST(GETDATE() AS DATE));
In SQL Server 2008 and above just do this:
SELECT DATEADD(day, 1, Getdate()) AS DateAdd;
can try this
select (CONVERT(VARCHAR(10),GETDATE()+360,110)) as Date_Result
Two or three ways (depends what you want), say we are at Current Date is
(in tsql code) -
DECLARE #myCurrentDate datetime = '11Apr2014 10:02:25 AM'
(BTW - did you mean 11April2014 or 04Nov2014 in your original post? hard to tell, as datetime is culture biased. In Israel 11/04/2015 means 11April2014. I know in the USA 11/04/2014 it means 04Nov2014. tommatoes tomatos I guess)
SELECT #myCurrentDate + 360 - by default datetime calculations followed by + (some integer), just add that in days. So you would get 2015-04-06 10:02:25.000 - not exactly what you wanted, but rather just a ball park figure for a close date next year.
SELECT DateADD(DAY, 365, #myCurrentDate) or DateADD(dd, 365, #myCurrentDate)
will give you '2015-04-11 10:02:25.000'. These two are syntatic sugar (exacly the same). This is what you wanted, I should think. But it's still wrong, because if the date was a "3 out of 4" year (say DECLARE #myCurrentDate datetime = '11Apr2011 10:02:25 AM') you would get '2012-04-10 10:02:25.000'. because 2012 had 366 days, remember? (29Feb2012 consumes an "extra" day. Almost every fourth year has 29Feb).
So what I think you meant was
SELECT DateADD(year, 1, #myCurrentDate)
which gives 2015-04-11 10:02:25.000.
or better yet
SELECT DateADD(year, 1, DateADD(day, DateDiff(day, 0, #myCurrentDate), 0))
which gives you 2015-04-11 00:00:00.000 (because datetime also has time, right?). Subtle, ah?
This will give total number of days including today in the current month.
select day(getDate())
Add Days in Date in SQL
DECLARE #NEWDOB DATE=null
SET #NEWDOB= (SELECT DOB, DATEADD(dd,45,DOB)AS NEWDOB FROM tbl_Employees)
SELECT DateAdd(5,day(getdate()) this is for adding 5 days to current days.
for eg:today date is 23/08/2018 it became 28/08/2018 by using the above query

Resources