SQL queries whether the current server time is within the table T1 time period, and if so, select the

Source: Internet
Author: User

Table T1
Start time End time
09:00:00-12:00:00
14:00:00-17:00:00

Query whether the current server time is within the table T1 time period, if any, select the
The selected SQL result set is as follows, SQL, note: SQL2000 environment

Start time End time
14:00:00-17:00:00

Method One:

DECLARE @t  TABLE (      beginTime  CHAR (8),      endTime  CHAR (8) ) INSERT INTO @t  values ( ‘09:00:00‘ , ‘12:00:00‘ ) INSERT INTO @t  values ( ‘14:00:00‘ , ‘17:00:00‘ ) SELECT FROM @t  WHERE CONVERT ( CHAR (8), GETDATE(),114)  BETWEEN beginTime  AND endTime /* beginTime   endTime 14:00:00    17:00:00 */Method Two: IF object_id (' tempdb. #T1 ') is not a NULL DROP TABLE #T1
CREATE TABLE #T1 (id INT IDENTITY, start time NVARCHAR (50), End time NVARCHAR (50))

INSERT into #T1
(Start time, end time)
VALUES (' 09:00:00 ', ' 12:00:00 ')
INSERT into #T1
(Start time, end time)
VALUES (' 14:00:00 ', ' 17:00:00 ')

SELECT * from #T1 WHERE
Convert (datetime, ' 1900-01-01 ' + start Time) <=convert (datetime, ' 1900-01-01 ' +substring (CONVERT (NVARCHAR), GETDATE () , 21), 12,50))
and
Convert (datetime, ' 1900-01-01 ' + End time) >=convert (datetime, ' 1900-01-01 ' +substring (CONVERT (NVARCHAR), GETDATE () , 21), 12,50))

SQL queries whether the current server time is within the table T1 time period, and if so, select the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.