How to increase the duration logic in SQL SP - sql

I have a stored procedure which deletes the records from DB based on following logic.
Select Id into #deleterecords from pricetable where
((Convert(date, modifiedon) <> convert(date,gatedate())
I would like to add one more day in Gatedate()
I’m trying in this
Select Id into #deleterecords from pricetable where
((Convert(date, modifiedon) < > convert(date,gatedate()+1)
It’s not working.
How can I add one more day?

What do you mean by "It’s not working."? Are you getting some error message?
Did you mean to write GETDATE() (the built-in function that returns the current timestamp), or you indeed have a gatedate() function in your database?
To add a day to a datetime value in SQL Server use the function DATEADD.
select Id
into #deleterecords
from pricetable
where
convert(date, modifiedon) <> convert(date, DATEADD(day, 1, gatedate()))
;

Related

Stored procedure - to print my result from SQL query

select count(*) as count
from [dbo].[DATA_received] where DATEDIFF(MINUTE, DATE_TIME GETDATE()) <= 1
I want to run this in some frequency to check whether I am receiving data in DB.
I want to write stored procedure where if my results 0 then ,"No data received"
If the count is >0 then it should print "Data received"
Not sure how you're going to make SolarWinds do something with this output or run it every minute, but there is absolutely no reason to count how many rows were added in the last minute if you only need to know whether at least one such row exists... and you really should never apply formulae like DATEDIFF to a column, it will force the query to scan the entire table every time:
SELECT MessageToPrint = CASE WHEN EXISTS
(
SELECT 1 FROM dbo.DATA_received
WHERE DATE_TIME >= DATEADD(MINUTE, -1, sysutcdatetime())
) THEN 'Data received.' ELSE 'No data received.' END;

How to Update end date by adding duration to start date SQL

As in title i don't know how to add duration (from the same table) to start date, to get end date in result, table is looks like:
create table NR1
(
id INTEGER not null,
price INTEGER,
price2 INTEGER,
start_date DATE,
end_date DATE,
duration NUMBER
)
i tried something like this, but i have some errors:
update nr1
set end_date =dateadd(MONTH, nr1.duration, nr1.start_date);
it should works, but i have problems with MONTH, at all SQL developer says that.
You probably need add_months:
update NR1
set end_date = add_months(start_date, duration)
You are using syntax form a different database platform. The Oracle equivalent is the add_months() function:
update nr1
set end_date = add_months(nr1.start_date, nr1.duration);
If you just add a plain number to a date that's treated as a number of (whole or partial) days. You can also use intervals (via numtodsinterval()) for other periods of time, but using that for months can be difficult.
Months and days are simple through the basic functionality and functions though.
If you're on a recent version of Oracle you should consider using a virtual column so you don't have to maintain both values.
These are perfectly working SQL statement.
SELECT DATEADD(HOUR, 5, '5:00');
SELECT DATEADD(MINUTE, 5, '5:00');
SELECT DATEADD(SECOND, 5, '5:00');
You can try queries here: W3Schools Link

Update all date columns in SQL Server -1 day

I want to update my database (SQL Server Express) all the dates for specific ids.
I am displaying the ids I want to.
SELECT TOP (1000) ID, Dates
FROM tbl_table
WHERE (id IN (29695, 29700, 29701, 29702, 29703, 29704, 29705, 29706, 29707, 29708, 29709, 29710, 29711, 29712, 29713, 29714, 29715))
AND my dates in the database are like this:
Is there any way to update all the date columns with same date - 1 day?
For example: if we have 2019-12-20, update it to 2019-12-19?
For example if I want to do it in PHP, I would loop through this query and fetch all all dates. After I would remove one day like this:
date('m/d/Y', strtotime($date. ' - 1 days');
And create a query to update all the columns based on id. I just want to avoid that. Is there any SQL command that do that?
Thanks
The request below will update the rows you want by adding -1 days on each date:
UPDATE tbl_table
SET dates = Dateadd(day, -1, dates)
WHERE id IN ( 29695, 29700, 29701, 29702,
29703, 29704, 29705, 29706,
29707, 29708, 29709, 29710,
29711, 29712, 29713, 29714, 29715 )
DATEADD function takes 3 parameters:
the interval ( day, month, year ..)
the increment (the value to add or remove if negative)
an expression (wich is a datetime type)
See DATEADD documentation
To return a query with the previous day:
SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
FROM tbl_table
To update the table with the previous day:
UPDATE tbl_table
SET Dates = DATEADD(dd, -1, Dates)
FROM -- Put your conditions here
UPDATE tableName SET date= DATEADD(d,-1, date)
where ....
( here you put where clause for you required)

How can I group by day, and still return a datetime?

I want to track the users in my db, when they was created to show it in a awesome chart. Each user has a column "Created" that is the DateTime when they was created. Right down to the time that day.
However, for my chart I dont really care about the time, just the day, month and year. Is there a way I can return a datetime and count when I use datepart as the following:
SELECT datepart(year,Created), datepart(month,Created), datepart(day,Created), COUNT(*) AS COUNT
FROM [dbms].[User]
GROUP BY datepart(year,Created), datepart(month,Created), datepart(day,Created)
This returns three columns for year, month and day. Is there any way I could make it sexy and make it return DateTime (in YYYY/MM/DD format) and the cound?
If you're using SQL Server 2008 or later, you can take advantage of the date data type.
SELECT cast(Created as date), COUNT(*) AS COUNT
FROM [dbms].[User]
GROUP BY cast(Created as date)
If you're using SQL Server 2005 or earlier:
SELECT dateadd(day,datediff(day,0,Created), 0), COUNT(*) AS COUNT
FROM [dbms].[User]
GROUP BY dateadd(day,datediff(day,0,Created), 0)
You can try the following:
SELECT dateadd(day, datediff(day, 0, Created), 0) as date, COUNT(*) AS COUNT
FROM [dbms].[User]
GROUP BY dateadd(day, datediff(day, 0, Created), 0)
This will group you users by the creation date without time and will works on each versions of SQL Server. Among this, the Dateadd operation is more faster that casting...

Select from table given day

So I have a table in SQL Server with a datetime column on it. I want to select all from this table:
select * from dbo.tblMessages
but I want to pass in a datetime parameter. Then I want to select all messages from the table that have the same day as the datetime column in tblMessages, not just ones posted in the past 24 hours, etc.
How would I do this?
Thanks.
This should use an index on MyDateTimeCol in tblMessages
select * from dbo.tblMessages
WHERE
MyDateTimeCol >= DATEADD(day, DATEDIFF(day, 0, #Mydatetimeparameter), 0)
AND
MyDateTimeCol < DATEADD(day, DATEDIFF(day, 0, #Mydatetimeparameter), 1)
Any function applied to MyDateTimeCol will prevent an index being used correctly, includin DATEDIFF between this and #Mydatetime
As you are on SQL Server 2008 you can just do
SELECT *
FROM tblMessages
WHERE CAST(message_date AS DATE) = CAST(#YourDateParameter AS DATE)
This is sargable. SQL Server will add a ComputeScalar to the plan that calls the internal GetRangeThroughConvert function and gets the start and end of the range to seek.
If you need to do this a lot, and if you're on SQL Server 2005 or newer, you could also do this:
add three computed columns for the day, month, year of your date and persist those
ALTER TABLE dbo.YourTable
ADD DayPortion AS DAY(YourDateTimeColumn) PERSISTED
-- do the same for MONTH(YourDateTimeColumn) and YEAR(YourDateTimeColumn)
put an index on the three columns:
CREATE NONCLUSTERED INDEX IX_DatePortions
ON dbo.tblMessages(YearPortion, MonthPortion, DayPortion)
now, you can search very easily and quickly for those days, months, year, and with the index, your search will be very performant and quick
SELECT (list of columns)
FROM dbo.tblMessages
WHERE YearPortion = 2011 AND MonthPortion = 4 AND DayPortion = 17
With this setup - three computed, persisted columns - you can now simply insert new rows into the table - those three columns will be calculated automatically.
Since they're persisted and indexed, you can easily and very efficiently search on those columns, too.
And with this flexibility, you can also easily find e.g. all rows for a given month or year very nicely.