-7

I want to write a sql which take two input time as string format. i just check the time so that two classes in school will not overlapped fully or partially both. for ex: if the time 9:00Pm - 10:00PM already in database then it prevents to insert. so that can't insert like as : 8:00PM- 12PM. but it can insert from 10:00PM -12PM. Also check AM/PM.

how i handle it by SQL.

sufian ahmed
  • 31
  • 1
  • 8

1 Answers1

0

You can create a Primary Key Constraint by creating a composite index with the two time values together. For example:

CONSTRAINT PK_tTimeFrom_tTimeTo PRIMARY KEY NONCLUSTERED ([tTimeFrom], [tTimeTo])

This example is for MS SQL.

Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107
cyclington
  • 48
  • 6
  • 1
    Yes, but no. That would solve only his problem of overlapping and matching dates, but what if the start and end times do not match, but overlap with other records? Then the `INSERT` will be successful, but it shouldn't. – Radu Gheorghiu Nov 17 '16 at 07:34
  • Ah True, i mis-interpreted the question! – cyclington Nov 17 '16 at 07:50