How to update dates in a table so that each row's date is pushed ahead by 1 day? [closed] - sql-server

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
I have a table whose Date column needs pushing ahead by 1 day.
My update query is:
UPDATE TABLENAME
SET DATECOL=DATECOL+1
Is this correct approach? Or do I need to use CTE, for example:
;WITH CTE AS (
SELECT ID, DATECOL
FROM TABLENAME)
UPDATE T
SET T.DATECOL=CTE.DATECOL+1
FROM TABLENAME T
JOIN CTE ON T.ID=CTE.ID

To add a value to any part of date you can use DATEADD function. In your case the part time is DAY.
UPDATE TABLENAME
SET DATECOL=DATEADD(DAY, 1, DATECOL)

Related

T-SQL how to properly union date and time in datetime [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 21 hours ago.
This post was edited and submitted for review 10 hours ago.
Improve this question
dear gurus, SQL.
Please advise how to do the following:
You need to take a date from one column and join it with the time of another column,
and then add one day to the received date.
I wrote such a query, but on some data it gives a conversion error.
Please tell me a more optimal query so that it always works when combining the date and time format into datetime.
Thank you.
create table dt (date1 datetime, date2 datetime)
insert into dt values('19000101 17:17:00.000','19070101 17:51:00.000')
insert into dt values('19000101 18:20:00.000','19080101 18:21:00.000')
insert into dt values('20000101 06:00:00.000','20100101 06:40:00.000')
select
dateadd(dd,1,convert(datetime,convert(date,date1))+ convert(datetime,convert(time, date2)))
from dt
Here is an example where such an error could occur:
CREATE TABLE Example (
Duration TIME,
AdditionalDelay SMALLINT
)
INSERT INTO dbo.Example (Duration, AdditionalDelay)
VALUES ('00:01:02',3)
SELECT Duration+AdditionalDelay FROM dbo.Example
To fix this, you need to identify the proper unit of measure for AdditionalDelay and use DATEADD instead of +. For example:
SELECT DATEADD(MINUTE,AdditionalDelay,Duration) FROM dbo.Example

How to delete top (1) row from SQL if row count reached to 10000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have made fault history log into HMI through SQL database,
I am deleting top row by PLC TAG by using following code
DELETE TOP (10) PERCENT FROM [dbo].[ABC]
I want to delete first row while my row-count reached to 10000
in other way to say that ....
a new fault record add in log and first old record should delete and so on...
If I understansd you correctly, you need to number the rows using ROW_NUMBER() and then execute appropriate DELETE and INSERT statements:
DELETE t
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY PLC DESC) RN
FROM [dbo].[ABC]
) t
WHERE RN >= 10000
INSERT [dbo].[ABC] (PLC) VALUES (...)

How to give system date while creating SQL table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
SQL Server creating table error.
How to give system date, system date + 2 days while creating a table?
Is the system date the same as the current date?
You can try the SQL DEFAULT Constraint as shown below if I understood your question properly:
Create table Test(id int
, name varchar(20)
, dtDate datetime default DateAdd(dd, 2, getdate()))
insert into Test (id, name) values
(1, 'A')
select * from Test
Live db<>fiddle demo.

Pivot selected columns into rows with SQL Server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a column-based table like this:
And I need to convert it to a row-based table like this:
Any help would be appreciated.
I think you need an UNPIVOT -
SELECT OrgID, Property, DataValue
FROM (SELECT OrgID, Property, DataValue
FROM YOUR_TABLE
UNPIVOT
(
DataValue FOR Property IN (ExtensionDigits
,StartExtension
,LastUsedExtension
,Server1IP
,Server2IP
,ServerPort
,IsFunctionOn
,VolumeDecibel)
) unpvt) temp
WHERE DataValue IS NOT NULL;

I need a query to delete a value in table if value is older than 4 months please look at details [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a EmailID_tbl that has email address of the users I have got request to "remove emails from EmailID_tbl if last login date for user is within the past 4 months."
Assuming that these are in the same table, you could just use a WHERE clause to determine what needs to be deleted via the DATEADD() function to subtract the appropriate number of months from GETDATE(), which would yield the current date :
-- This would delete every record in your table with LastLoginDates
DELETE
FROM EmailID_tbl
WHERE LastLoginDate < DATEADD(month,-4,GETDATE())
Here's a query that will do it:
DELETE
FROM EmailID_tbl
WHERE LastLoginDate <= DATEADD(mm, -4, GETDATE())
Change LastLoginDate to the actual name within your table which indicates the last login date, and change <= to >= if you actually mean to delete more active users instead of more inactive users.

Resources