Click here to Skip to main content
15,921,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table called Customer with a sample Data

CODE FM_DT TO_DT

A 1-Oct-2010 30-Oct_2010

Before each insert of Record A, I want to check the FM_DT and TO_DT, and make sure new entered date is not the equal to existing date and does not belong existing date range.

eg: It's not possible to add 2-Oct-2010 to 5-Oct-2010 for record A but if user try to insert the query should return a value.
I write a query but it allows duplicating.

Any solution from you?
this is my query


C++
SELECT 1

FROM OM_MERCH_CUST

WHERE MC_CUST_CODE = :NEW.MC_CUST_CODE

AND ((OM_MERCH_CUST.MC_FM_DT BETWEEN :NEW.MC_FM_DT AND :NEW.MC_TO_DT)

OR OM_MERCH_CUST.MC_TO_DT BETWEEN :NEW.MC_FM_DT AND :NEW.MC_TO_DT );
Posted
Updated 27-Oct-10 22:40pm
v3
Comments
Dalek Dave 28-Oct-10 4:40am    
Edited for Grammar, Spelling, Syntax and Readability.

This code should be working for the the sample that you give.
SQL
SELECT 1
FROM OM_MERCH_CUST
WHERE MC_CUST_CODE = :NEW.MC_CUST_CODE
AND ((OM_MERCH_CUST.MC_FM_DT < :NEW.MC_FM_DT)
AND (OM_MERCH_CUST.MC_TO_DT > :NEW.MC_TO_DT));
 
Share this answer
 
Try to do this it will add allot more validation and will eliminate the chance of a user entering the same date range or a date in the already set range.

and ((OM_MERCH_CUST.MC_FM_DT NOT BETWEEN :NEW.MC_FM_DT AND :NEW.MC_TO_DT)
or  (OM_MERCH_CUST.MC_TO_DT NOT BETWEEN :NEW.MC_FM_DT AND :NEW.MC_TO_DT)
or OM_MERCH_CUST.MC_FM_DT <> :NEW.MC_FM_DT
or OM_MERCH_CUST.MC_TO_DT <> :NEW.MC_TO_DT);
 
Share this answer
 
XML
''' <summary>
   ''' Check a Date Range exist in a Table's 2 date columns
   ''' </summary>
   ''' <param name="ToTestFromDate">From Date value from User Form</param>
   ''' <param name="ToTestToDate">To Date value from User Form</param>
   ''' <param name="FromDateColumnName">From Date Column Name in Table</param>
   ''' <param name="ToDateColumnName">To Date Column Name in Table</param>
   ''' <param name="TableName">Table Name of where From,To Date Column Exists</param>
   ''' <param name="DateParamBraces">"#" for Access, "'" for SQL</param>
   ''' <param name="Condition">Give your Where condition here.  "" for no condition. ELSE - " and FieldName = Value"</param>
   ''' <returns>Boolean</returns>
   ''' <remarks></remarks>


Public Function DateRangeFallsBetween2FldsOfaTable(ByVal ToTestFromDate As DateTime, ByVal ToTestToDate As DateTime, _
ByVal FromDateColumnName As String, ByVal ToDateColumnName As String, ByVal TableName As String, _
ByVal DateParamBraces As String, Optional ByVal Condition As String = "") As Boolean

If Not Condition = String.Empty Then
Condition = " and " & Condition
End If

Dim Query As String
Dim dtTemp As New DataTable

Query = "SELECT " & FromDateColumnName & ", " & ToDateColumnName & ", count(*) as TotalRecords FROM " & TableName & " " & _
" where (((" & FromDateColumnName & " between " & DateParamBraces & ToTestFromDate & DateParamBraces & _
" and " & DateParamBraces & ToTestToDate & DateParamBraces & ") or (" & ToDateColumnName & " between " & DateParamBraces & ToTestFromDate & DateParamBraces & _
" and " & DateParamBraces & ToTestToDate & DateParamBraces & ")) or ((" & DateParamBraces & ToTestFromDate.ToString("dd/MMM/yyyy") & DateParamBraces & " between " & FromDateColumnName & " " & _
" and " & ToDateColumnName & " " & ") or (" & DateParamBraces & ToTestToDate.ToString("dd/MMM/yyyy") & DateParamBraces & " between " & FromDateColumnName & " " & _
" and " & ToDateColumnName & " " & ")))" & _
Condition & _
" Group by " & FromDateColumnName & ", " & ToDateColumnName & " " & _
" order by " & FromDateColumnName & ""


dtTemp = CType(FetchData(Query, 0, "tblStaffMaster"), DataSet).Tables(0).Copy

DateRangeFallsBetween2FldsOfaTable = If(dtTemp.Rows.Count > 0, True, False)

If DateRangeFallsBetween2FldsOfaTable Then
MsgBox("Date Range Already Exist FOR " & Condition)
End If

End Function
 
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