Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for Exampple today date is 5/10/2013 i want a date as 4/11/2013 in sql server and VB.net plz help me
Posted

You have to use DATEADD Function of SQL Server.
Syntax :
SQL
DATEADD(DatePart, Number, Date)

See below Command which will give you the correct result :
SQL
SELECT DATEADD(MONTH,1,DATEADD(DAY,-1,GETDATE()))
Or
SQL
DECLARE @TDATE DATETIME
SET @TDATE = '2013-10-05' --You can pass your Parameter here.
SELECT DATEADD(MONTH, 1, DATEADD(DAY,-1, @TDATE))

To dynamically set date as Parameter you can use Parameterized SQL Query and execute this query through code :
VB
_mSqlCommand.CommandText = "SELECT DATEADD(MONTH, 1, DATEADD(DAY,-1, " & Date.Now.Date & "))"'You can pass your Parameter here.
Or in ASP.Net/VB.Net
VB
Dim _myDate As Date = "2013-10-05" 'You can pass your Parameter here.
MsgBox(DateAdd(DateInterval.Day, -1, _myDate.AddMonths(1)))

I hope this will help you. :)
 
Share this answer
 
Comments
Sachin MCA2012 6-Oct-13 14:57pm    
yes Manoj this will work fine but if the date is 2013-09-1 then output will show as 2013-09-30
but i want it as 2013-10-31 or 2013-10-30
Manoj K Bhoir 8-Oct-13 1:07am    
In your question you said that if date is 05/10/2013 then you want 04/11/2013
Difference between 05/11/2013 and 04/11/2013 is 30 Days
and now you are saying if date is 01/09/2013 then you want 31/10/2013
Difference between 01/09/2013 and 30/10/2013 is 60 Days.
So,
For your expected Result you have to add 2 Months instead of 1 in your query.
SELECT DATEADD(MONTH, 2, DATEADD(DAY,-1, @TDATE))
You just have to Check for what date which result you want. :)
Hi ,

Please use this in Ms sql server, it will give you desired result

Quote:
select dateadd("MONTH",1,GETDATE()-1 )


In VB.Net/ASP.net
MsgBox(DateAdd(DateInterval.Day,-1, Today.AddMonths(1) )

Hope this will solve your problem

Regards,
Mubin
 
Share this answer
 
v2
Comments
Sachin MCA2012 5-Oct-13 14:05pm    
but how can i pass date in this as a parameter-1
select dateadd("MONTH",1,GETDATE()-1 )
Mubin M. Shaikh 5-Oct-13 14:41pm    
From where you want to pass parameter. From asp.net /vb.net to sql?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900