Search code examples
sqlt-sqldatetimedateadd

How to filter in a single field using DateAdd


"FollowingTrialDate" type is datetime

I want to filter between dates in a single field.

How can I do filtering in the FollowingTrialDate field within the last fifteen days.

If there is an error, where is it?

Best Regards,

SELECT C.[No],
convert(date,CaseDate,103) as ModuleDate,
convert(date,FollowingTrialDate,103) as FollowingTrialDate,
ShortDescription,
Username,
Name,
Surname,
Email,
C.[Description] as [Desc],
'Davalar' as ModuleName
FROM [LegalIT_MnemonicTEST].[dbo].[Module_Case] C 
inner join [System_User] su on C.LawyerID = su.UserID 
where CONVERT(date,FollowingTrialDate,0) 
between DATEADD(DD,-15,CONVERT(date,FollowingTrialDate,0)) and 
        DATEADD(DD,0,CONVERT(date,FollowingTrialDate,0)) 

Solution

  • Try the below code (if the type of FollowingTrialDate is datetime specified in your db)

    Notice that if your CaseDate or FollowingTrialDate is already in type 'datetime' specified in your database, you don't need to convert it into date first for output or for filtering data used in your WHERE clause

    SELECT C.[No],
    convert(datetime,CaseDate) as ModuleDate,
    FollowingTrialDate,
    ShortDescription,
    Username,
    Name,
    Surname,
    Email,
    C.[Description] as [Desc],
    'Davalar' as ModuleName
    FROM [LegalIT_MnemonicTEST].[dbo].[Module_Case] C 
    inner join [System_User] su on C.LawyerID = su.UserID 
    where FollowingTrialDate
    between DATEADD(DAY, DATEDIFF(DAY, 0, dateadd(dd, -15, getdate())), 0) and 
            DATEADD(DAY, DATEDIFF(DAY, 0, getdate()), 0)