| |

VerySource

 Forgot password?
 Register
Search
View: 2451|Reply: 14

Ask time to deal with the problem

[Copy link]

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-7 18:00:01
| Show all posts |Read mode
Table tb1 structure:
    f1 int autoincrement
    f2 datetime

The value of f2 is roughly as follows:
2016-12-20 22: 22: 32.000
2016-12-20 23: 32: 38.000
2016-12-20 00: 24: 49.000

declare t1 char (5),
        t2 char (5),
        t3 datetime

set t1 = '23: 30 '
set t1 = '00: 30 '

Now how to find out the record of the time part of the field f2 in the range t1 to t2 in the table tb1
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-3-29 12:00:02
| Show all posts
select * from tablename where f2 between cast (('2016-12-20' + '23: 30 '+': 000 ') as datetime) and cast ((' 2016-12-21 '+ '00: 30' + ': 000') as datetime)
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 Unknown

Post time: 2020-3-29 20:00:01
| Show all posts
declare @a table (id int identity (1,1), f2 datetime)
insert into @a select '2016-12-20 22: 22: 32.000' union all
select '2016-12-20 23: 32: 38.000' union all
select '2016-12-20 00: 24: 49.000'
select * from @a where f2 between
cast (('2016-12-20' + '23: 30 '+': 000 ') as datetime)
and cast (('2016-12-21' + '00: 30 '+': 000 ') as datetime)
result:
id f2
----------- --------------------------------------- ---------------
2 2016-12-20 23: 32: 38.000

(1 row affected)
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-29 21:15:01
| Show all posts
select * from tablename
where convert (varchar (10), f2,108) between '23: 30 'and '00: 30'
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 United States

Post time: 2020-3-30 00:00:02
| Show all posts
create table T (f1 int, f2 datetime)
insert T select 1, '2016-12-20 22: 22: 32.000'
union all select 2, '2016-12-20 23: 32: 38.000'
union all select 3, '2016-12-20 00: 24: 49.000'

declare @ t1 char (5), @ t2 char (5)
set @ t1 = '23: 30 '
set @ t2 = '00: 30 '

select * from T
where f2 between (convert (char (10), f2, 120) + '' + @ t1) and (convert (char (10), f2 + 1, 120) + '' + @ t2)
--result
f1 f2
----------- --------------------------------------- ---------------
2 2016-12-20 23: 32: 38.000

(1 row (s) affected)
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-31 16:30:01
| Show all posts
fanfan1980:
Your sentence looks for the record of '2016-12-20 00: 24: 49.000'
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-31 19:15:01
| Show all posts
fanfan1980:
Your sentence could not find the record for '2016-12-20 00: 24: 49.000'
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-4-1 16:30:01
| Show all posts
Many time records:
2016-12-21 23: 47: 10.000
2016-12-21 23: 54: 19.000
2016-12-27 23: 55: 25.000
2016-12-23 23: 54: 14.000
2016-12-24 00: 04: 14.000
2016-12-24 23: 58: 42.000
.
.
.
.
.
.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-6 09:30:01
| Show all posts
create table #t (f1 int, f2 datetime)
insert #t select 1, '2016-12-20 22: 22: 32.000'
union all select 2, '2016-12-20 23: 32: 38.000'
union all select 3, '2016-12-20 00: 24: 49.000'

select * from #t where datepart (hh, dateadd (mi, 30, f2)) between 0 and 1
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Spain

 Author| Post time: 2020-4-7 18:45:01
| Show all posts
Still not good, you can also find out 2016/12/28 00:34:11
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list