如何检查时间范围内的时间范围并在SQL中获得冗余时间?

时间:2016-01-09 08:50:11

标签: sql-server

假设我有4个变量:

@start = 5 AM
@end = 8 AM
@from = 10 PM
@to = 6 AM

@from@to被称为工作时间。 @start@end被称为早晨时间。

如何使用SQL检查"工作时间"在早上的时间"?如果发生这种情况,我需要获得冗余时间。

我的意思是从早上5点到早上6点等于1小时。下图更好地解释了我的意思: enter image description here

1 个答案:

答案 0 :(得分:1)

伪代码是:

DateTime

当然,您了解变量必须为declare @from datetime = '2015-12-31 22:00' declare @to datetime = '2016-01-01 06:00' declare @start datetime = '2016-01-01 05:00' declare @end datetime = '2016-01-01 08:00' if ( ( @start < @to ) and ( @end > @from ) ) select datediff(hour, @start, @to) 类型,因为两个@from和@to跨越两天。

使用完整示例进行编辑

        include ("tcpdf/tcpdf.php");

        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        $pdf->SetPrintHeader(FALSE);
        $pdf->SetPrintFooter(FALSE);
        //these line i have added in file to include font
        $fontname = $pdf->addTTFfont('D:/wamp/www/projectname/sites/all/modules/civicrm/packages/tcpdf/fonts/Frutiger LT 55 Roman.ttf', 'TrueTypeUnicode', '', 32);
        $pdf->SetFont($fontname, '', 14, '', false);
        $pdf->AddPage('P', 'A4');

        $html = '<html>
                        <head>
                        <title>Insurance Certificate</title>
                        <style>
                        body{
                            font-family : Arial, Helvetica, sans-serif;
                        }
                        table{
                        table-layout: fixed;
                        }

                        </style>
                    </head>
                    <body style="-webkit-font-smoothing:antialiased;">
                        <table border="0px" align=""> 
                            <tr>

                            </tr>                    
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="text-align: left; font-size:14px;"></td>
                                <td width="200px"></td>
                            </tr>                    
                            <tr>
                                <td width="125px" style="border-collapse: collapse; border-bottom: 1px solid black;"></td>
                               <td width="200px"style="text-align: left; font-size:10px; border-collapse: collapse; border-bottom: 1px solid black; width:400px"><span style="font-size:8px">Mobile Phones, Tablets & Laptops Insurance Cover</span></td>
                                <td width="200px"></td>
                            </tr>
                            <br /><br />
                            <tr bgcolor="#EAEBEC"> 
                               <td width="125px"></td>
                               <td width="200px"  height="25px" style="text-align:left" ><b><br /> </td>
                               <td width="200px" style="display:none;" ></td>
                            </tr>
                            <br /><br />
                            <tr>
                                <td width="125px"  colspan="2" ></td>
                                <td width="300px" style="font-size:10px; text-align: justify; "></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left; ">Name of the Insured in full</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Address for communication</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>                            
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Contact Number & Email ID</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>                    
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left; ">Brand & Model of the Device</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Date of Purchase of the Device</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">IMEI / Serial No</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Sum Insured</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>    
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Invoice No / Certification No</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Insurance Plans</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>                        
                                <td></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Coverage Start Date & Time</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>
                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Coverage End Date</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>

                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Store Code</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>

                            </tr>
                            <tr>
                                <td width="125px"></td>
                                <td width="200px" style="font-size:10px; text-align: left;">Store Name</td>
                                <td style="font-size:10px; text-align: left; font-weight:bold;" width="200px"></td>

                            </tr>
                            <tr>
                                <td colspan="2"><img src="images/spacer.gif" width="2px" height="50px" /></td>
                            </tr>

                            <tr>
                                <td width="125px"  style="text-align:center; padding-left:50px;">

                                </td>
                                <td width="400px">
                                </td>

                            </tr>
                            <tr>
                                <td></td>
                                <td><img style="margin: 0 0 0 0; width:100px" src="images/edelweiss.png" /></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                            </tr>
                            </table>
                            <table>
                            <td width="20px"></td>
                            <td width="500px"><div style="display: block; overflow: hidden; font-size:8px; width: 100px">         
                            <span><img src="images/spacer.gif" width="10px" height="130px" /></span>
                            <p style="text-align: left; font-weight:bold; padding: 0; margin:0">Terms and Conditions</p>
                            <p style="text-align: left;"><b>Product / Device:</b> 
                            Mobile phone under the scheme of insurance. <br />
                            Value  The value equa on the invoice of Insured Product, bought by the end customer i.e “cost of the product” or “Market value” whichever is lower. Policy will be on agreed value basis subject to maximum of Invoice value.<br />
                            <b></b>: Damage to the Insured Product due to session of the insured person without the person’s consent. Theft will be defined as per relevant sections of the Indian Penal Code and will include but not be limited to burglary, housebreaking, robbery and dacoity. <br />
                            <b></b>: Sudden and accidentaed Product.                   
                            </p>
                            <p style="text-align: left; padding: 0;"><b>Specific Condition </b><br />
                            In the event of any damage which might give rise to a claim under the insurance policy you shall: <br />                     
                               &bull; Notify Insurance Co /Edelweiss within 24 Hours of loss <br />
                               &bull; Take all reasonable steps to minimize the extent of any damage <br />
                               &bull; Preserve damaged parts and make them available for inspection by insurance company representative and/or insurance surveyor and/or authorized representative of service centre of OEM<br />
                               &bull; Provide all information and documentary evidence with respect to the claim as insurer may reasonably require<br />
                                <br />                       
                            <p style="text-align: left; "><b>Depreciation Rates:</b><br />
                            The liability of the Company shall in no case exceed the sum insured subject to the following as per Age of the product / device: <br />
                            &bull;Upto 90 days:10% &bull; 91 to 180 days:15% &bull; 181 to 270 days:20% &bull; 271 to 365 days:25%  &bull; Above 365 days:40%
                            </p>
                      </div></td>

                        </table>
                    </body>
                </html>';


        //echo $html;

        $pdf->writeHTML($html, true, false, true, false, '');
        $pdf->lastPage();
        $pdf->Output();
        ?>

结果= 1