hi all.i have 2 dates in string format.i need to add these 2 dates to get the next date. is there any sql query for this?? Plz sum1 help me. urgent!! Thanx in advance...

Dani AI

Generated

The phrase "add 2 dates" is ambiguous. Possible intents are: add a day-count (duration) to a date; add two durations together; or pick the later of two date values. needs to decide which of those is meant before choosing a reliable approach. rightly flagged the forum mismatch, and was on the right track about parsing the strings first — the rest is just safe parsing + the correct operation.

For a common case (string date + string day-count), parse the date explicitly and then add days in code. Example in VB.NET:

Dim sDate As String = "2025-01-01"
Dim sDays As String = "7"
Dim dt As DateTime

If DateTime.TryParseExact(sDate, "yyyy-MM-dd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
    Dim days As Integer
    If Integer.TryParse(sDays, days) Then
        Dim nextDate As DateTime = dt.AddDays(days)
        ' nextDate is the result
    End If
End If

In SQL Server, use safe parsing and a day-add operation while guarding against bad input. Example:

DECLARE @dateString NVARCHAR(20) = '2025-01-01';
DECLARE @daysString NVARCHAR(10) = '7';

SELECT DATEADD(day, TRY_CAST(@daysString AS int), TRY_CONVERT(date, @dateString, 23)) AS NextDate;

Troubleshooting notes: prefer ISO format (yyyy-MM-dd) when exchanging strings, use the TRY_* functions to avoid exceptions, validate NULLs and non-numeric durations, and store dates and durations in proper typed columns (date/int) so you only parse once. If the goal is "later of two dates", use a simple comparison (CASE in SQL or a conditional in .NET) instead of arithmetic.

Recommended Answers

All 2 Replies

you posted this in the asp forum, but you are asking for sql

how are you wanting to add dates?
you can add dates together, but i don't really know what good that would do for you

be more clear on what you want

if you want to just add days lets say

dateadd(d,1,getdate())

hi guys

First convert both string into date format
then two dates can be add in vb.net using function.
So for in ASp.net forum u need to right customised function.
Same in SQL for Ms SQL SERVER using procedure u can convert strings to a date format
and then using datediff & dateadd function user can add to diff. dates are in string format

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.