Add days to a date and insert added date in table for SQL SERVER 2012 - sql

#Rdatetime ='2017-04-18 11:49:38.633'
#Date to add = 2
#Output = '2017-04-20 11:49:38.633'
Hi all,
This is my Problem , i am getting Rdatetime and date to be added in another table . i have yo add the date and save it in datetime format
thanks in advance

Use DATEADD built in function :
DECLARE #Rdatetime DATETIME ='2017-04-18 11:49:38.633'
DECLARE #Date INT = 2
DECLARE #Output DATETIME = DATEADD(DAY,#Date,#Rdatetime)
SELECT #Output

You can make use DATEADD function by giving D as the first parameter.Then the number of days you want to add and the date to which you want to add.
SET #Output=DATEADD(D,#Date,#Rdatetime)

Related

Declare a variable which converts Date data type to Integer

In SQL Server, the following code gives me the date of Sunday last week from whatever date it is today:
DECLARE #dt date = GETDATE();
SELECT dateadd(week,datediff(week,6,#dt),6);
I need to declare a variable from the date given and make that variable an integer. I have tried things like:
DECLARE #last_sunday INT;
SELECT dateadd(week,datediff(week,6,#dt),6) INTO #last_sunday FROM xxx;
But the problem is that I don't understand which table I should write after FROM. In my interpretation the line:
dateadd(week,datediff(week,6,#dt),6)
is some sort of calculation rather than collecting a value from a data table. How can I make last Sunday's date into a variable that is an integer?
You need to do something like this:
Step 1
DECLARE #date DATE = dateadd(week,datediff(week,6,GETDATE()),6)
Step 2
DECLARE #dateInt AS INT = REPLACE(#date, '-', '')
In the first step, you are getting the correct date value in a date data type. Given that the date has the hyphens, you are then using the REPLACE function in step 2 to remove them and at the same time converting your date to an integer value.
Finally, to make sure that steps 1 and 2 work, you can run the following query:
SELECT #dateInt
For today, this will get you the following value: 20210530
To make life easier, onw could also create a function:
CREATE FUNCTION LastSunday ()
RETURNS int
AS
BEGIN
RETURN convert(int,dateadd(week,datediff(week,6,getdate()),6))
END
GO
After this you can do:
select dbo.LastSunday();
Which will (now 3th jun 2021) result in: 44344
simplest query:
select cast(replace(cast(getdate() as date),'-','') as int) as DateToInteger

Incremental next month dates in SQL

I have similar requirement, i have file feeding to SQL Server 2012 for a month but i wanted to convert the same file as a next month file.
For Example I have below
02/22/2018 02/23/2018 02/24/2018
and I want 03/22/2018 03/23/2018 02/24/2018
I am able to get upto this point but facing issue when i have below situation.
02/28/2018 02/24/2018
and I want 03/31/2018 03/31/2018
I have a normal calendar set up which I can use Can somebody please help.
you can use DATEADD() function for this like below :
SELECT DATEADD(month, 1, '02/22/2018') AS DateAdd;
I guess you are looking for EOMONTH function
somethink like this might help you solve your issue
DECLARE #date VARCHAR(10) = '02/28/2019'
SELECT CASE WHEN (EOMONTH(#date) = FORMAT(CAST(#date AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,#date))
ELSE DATEADD(month,1,#date)
END
Note: I have used format function because EOMONTH returns date in 'yyyy-MM-dd' format
UPDATE
DECLARE #dates TABLE
(
FromDate DATETIME,
ToDate DATETIME
)
INSERT INTO #dates (FromDate)
VALUES('12/31/2017'),('1/31/2018'),('2/28/2018'),('3/31/2018')
select * from #dates
UPDATE #dates
SET ToDate =CASE WHEN (EOMONTH(FromDate) = FORMAT(CAST(FromDate AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,FromDate))
ELSE DATEADD(month,1,FromDate)
END
SELECT * FROM #dates
Run this and you can see the final output

SQL Server date conversion is 68 years behind the actual date

I want to convert an integer to a date format. I am using the following script:
SELECT CAST(7549 as datetime)
It returns me a date 1920-09-02
However, 7549 actual date is 1988-08-31
Can anyone please help me how to perform correct mapping?
See the reverse
DECLARE #YourDate AS Datetime
SET #YourDate = '1920-09-02'
SELECT CAST(#YourDate AS INT) --7549
SET #YourDate = '1988-08-31'
SELECT CAST(#YourDate AS INT) -- 32384
If you want the result as '1988-08-31 00:00:00.000', try with the following scripts.
SELECT CAST(7549 as datetime)-CAST('1832-01-03' as datetime)
OR
SELECT CAST(7549 as datetime)+CAST(24835 as datetime)

Get addition of tow time in sql server 2008

I want to get Addition with tow time.
Start Time - 08:30:00
Addition Time - 04:45:00
i want to get
New Time - 13:15:00
How can i do that in sql server 2008.
Thank you.
Try this:
declare #start datetime = '08:30:00'
declare #add datetime = '04:45:00'
declare #result varchar(10)
select #result = substring(convert(varchar(24), convert(datetime, #start+#add, 121), 121),12,8)
If your start and added times are not datetime values, you need to cast them accordingly first.

Convert Varchar to DateTime - Varchar has no character to divide date parts

Is it possible to convert a varchar of 5122012 to a datetime of 05/12/2012?
Thanks
First of all you can't do it without trailing zeros of day and month. So your input data must be something like:
05122012
Second it is better to use the "native" format of:
20121205
what the server understands anyway
If it is the only way to get the data as you presented you would need a function to test and return the date
Basic function would be like this (SQL Server 2008 example):
This function works if the input data is: 05122012 if you need without trailing 0 you need to add the checks for that to the function
create function [dbo].[Str2Date] ( #data as varchar(8))
returns datetime
AS
begin
declare #day char(2), #mon char(2), #year char(4)
set #day = substring(#data,1,2)
set #mon = substring(#data,3,2)
set #year = substring(#data,5,4)
set #data = #year+#mon+#day
return convert (datetime,#data,112)
end
select dbo.Str2Date('05122012')