Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

Lấy danh sách các ngày theo thời gian truyền vào SQL Server

 Phần khó nhất khi làm việc với ngày tháng là đảm bảo rằng định dạng của ngày bạn đang cố gắng chèn khớp với định dạng của cột ngày tháng trong cơ sở dữ liệu.

MySQL comes with the following data types for storing a date or a date/time value in the database:

  • DATE - format YYYY-MM-DD
  • DATETIME - format: YYYY-MM-DD HH:MI:SS
  • TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
  • YEAR - format YYYY or YY

SQL Server comes with the following data types for storing a date or a date/time value in the database:

  • DATE - format YYYY-MM-DD
  • DATETIME - format: YYYY-MM-DD HH:MI:SS
  • SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
  • TIMESTAMP - format: a unique number

Note: The date types are chosen for a column when you create a new table in your database!



Trường hợp 1: 

WITH mycte AS

(

  SELECT CAST('2021-01-01' AS DATETIME) DateValue

  UNION ALL

  SELECT  DateValue + 1

  FROM    mycte   

  WHERE   DateValue + 1 < '2021-12-31'

)

SELECT  DateValue

FROM    mycte

OPTION (MAXRECURSION 0)

Or

select

  dateadd(day, nbr - 1, convert(date, '2017-01-01')) as d

from (

  select row_number() over (order by c.object_id) as nbr from sys.columns c

) nbrs

where

  nbr - 1 <= datediff(

    day,

    convert(date, '2017-01-01'),

    convert(date, '2018-12-31')

  )

Kết  quả: 2021-01-01 00:00:00.000

2021-01-02 00:00:00.000

2021-01-03 00:00:00.000

.............

2021-12-28 00:00:00.000

2021-12-29 00:00:00.000

2021-12-30 00:00:00.000

Trường hợp 2:

CREATE FUNCTION [dbo].[fc_get_range_day] (@StartDay Date, @EndDay Date)

RETURNS @tblVal TABLE

(

Id DATE

,time_period nvarchar(50)

)

AS

BEGIN

WHILE(@StartDay <= @EndDay)

BEGIN

INSERT @tblVal SELECT @StartDay AS Id,  CAST(DAY(@StartDay) as nvarchar) + ' tháng ' + CAST(month(@StartDay) as nvarchar)  AS time_period 

SET @StartDay = DATEADD(day, 1,@StartDay)

END

RETURN 

END

-- select * from [fc_get_range_day]('2019-12-20','2019-12-30')

Kết quả: 

2019-12-20 20 tháng 12

2019-12-21 21 tháng 12

2019-12-22 22 tháng 12

2019-12-23 23 tháng 12

2019-12-24 24 tháng 12

2019-12-25 25 tháng 12

2019-12-26 26 tháng 12

Trường hợp 3: Lấy dánh sách thời gian ngày giờ truyền vào các khun giờ

CREATE function [dbo].[fc_get_range_day_range_hour] (@StartDay Date, @EndDay Date,@start_hour int, @end_hour int)

returns @tblVal table

(

id_day DATE

,time_day nvarchar(50)

,id_hour int

,time_hour nvarchar(50)

)

as

begin 

insert @tblVal select Id as id_day, time_period as time_day, a.id_hour as id_hour,a.time_hour as time_hour from [fc_get_range_day] (@StartDay,@EndDay) 

CROSS JOIN  ( select id as id_hour ,time_period as time_hour  from [fc_get_range_hour] (@start_hour,@end_hour)) a

return 

END

-- select * from [fc_get_range_day_range_hour]('2019-12-20','2019-12-25',8,22) 

Kết quả:

2019-12-20 20 tháng 12 8 8:00 AM

2019-12-20 20 tháng 12 9 9:00 AM

2019-12-20 20 tháng 12 10 10:00 AM

2019-12-20 20 tháng 12 11 11:00 AM

2019-12-20 20 tháng 12 12 12:00 PM

2019-12-20 20 tháng 12 13 1:00 PM

2019-12-20 20 tháng 12 14 2:00 PM

2019-12-20 20 tháng 12 15 3:00 PM

2019-12-20 20 tháng 12 16 4:00 PM

2019-12-20 20 tháng 12 17 5:00 PM

Trường hợp 4: Lấy giờ theo giờ truyền vào

CREATE function [dbo].[fc_get_range_hour] (@start_hour int, @end_hour int)

returns @tblVal table

(

id int

,time_period nvarchar(50)

)

as

begin 

while(@start_hour <= @end_hour)

begin

if (@start_hour < 12)

insert @tblVal SELECT @start_hour, Case When @start_hour = 0 Then CAST('12' as nvarchar) + ':00 AM' else CAST(@start_hour as nvarchar) + + ':00 AM' End    

else if (@start_hour >= 12)

insert @tblVal SELECT @start_hour, Case When @start_hour = 12 Then CAST(@start_hour as nvarchar) + ':00 PM' else CAST(@start_hour - 12 as nvarchar) + ':00 PM' End    

set @start_hour = @start_hour + 1


--insert @tblVal SELECT @start_hour, Case When @start_hour < 10 Then '0'+ CAST(@start_hour as nvarchar) + ':00'    else  CAST(@start_hour as nvarchar) + ':00'    End  set @start_hour = @start_hour + 1

end

return 

END

 -- select * from [fc_get_range_hour] (8,22) 

Kết quả:

8 8:00 AM

9 9:00 AM

10 10:00 AM

11 11:00 AM

12 12:00 PM

13 1:00 PM



Đăng nhận xét

© Lưu Đức Huy IT. All rights reserved. Premium By Raushan Design