last day (14 days later) » 

11:04 AM
0
A: Replace only the date part

TT.DECLARE @target_date DATETIME ='2016-10-15 11:42:13.003'; DECLARE @to_day BIGINT = 5; SET @target_date=DATEADD(DAY,@to_day-DATEPART(DAY,@target_date),@target_date); SELECT @target_date; -- select result for verification This prints out 2016-10-05 11:42:13.003

 
Not working it adds the date not replacing it, I don't need to update it in table rather i need to store and see it in a variable
 
TT.
@MBalajivaishnav More correctly, it adds to the date. Its effect is replacing all datetime's whose date-part equals 2016-10-01, with 2016-10-15. The reason my answer adds days to the date you wish to replace, is that it's the most efficient way of doing this. Other solutions cast the date to string first, and fiddle with strings. That's less efficient than simply adding the difference in dates.
 
SET replace_date='20161015' does not be a DATETIME and also static one it is BIGINT
I need to replace the date part alone from the give date, I don't want to be updated in tables, I need to Store it in a variable
IF my given date is 2016/10/15 and the calculated BIGINT value is 5, here I need to replace the 5 in place of 15 so it will look like 2016/10/5
 
TT.
@MBalajivaishnav Perhaps I misunderstood the question... You are not seeking to update dates in a table? You just need to replace a variable date? I'll add that to my answer just in case that is what you want.
 
Now you got the question ?
 
TT.
11:04 AM
@MBalajivaishnav I think I did, updated answer, hope it's what you expected this time.
 
U there
 
TT.
Tell me,
 
check the link am getting the result as NULL
 
TT.
You have the dates in VARCHAR columns? That's a bad idea, better use DATETIME as type
 
No I changed it to BIGINT
Implicit conversion from data type datetime to bigint is not allowed. Use the CONVERT function to run this query.
am getting the error
 
TT.
11:07 AM
Why do you need these datetimes in BIGINT?
 
because it will calculated by subtracting two BIGINT Values
so am storing it in BIGINT
For example SET @ACTUALDUEDATE =@VAL - @VAL1 here @VAl and @VAL1 are BIGINT
 
TT.
DECLARE @actual_due_date INT = 5;
DECLARE @invoice_date DATETIME = '20161210';
DECLARE @due_date DATETIME;

SET @due_date=DATEADD(DAY,@actual_due_date-DATEPART(DAY,@invoice_date),@invoice_date);

SELECT @due_date;
You can change the type of @actual_due_date to BIGINT, no problem
Try that, I'm having problems running your script in Plunker. Try in SQL Server Management Studio
 
@invoice_date DATETIME = '20161210'; will be having like 2016/12/10
 
TT.
You need to start using the proper data-type for datatimes. Use DATETIME instead. If you have no control over the type, you will need to convert that VARCHAR to DATETIME first.
 
hey its working fine thanks plz update your anwer will accept and upvote it
 
TT.
11:18 AM
Done, I'm glad it works
Good bye
 

last day (14 days later) »