將它們存儲在datetime2中 - 它們應該以utc日期進入。如果你有一個字符串,使用CONVERT傳遞樣式代碼131
DECLARE @HijriDateString VARCHAR(10) = '02/02/1435';
declare @HijriDate datetime2 = CONVERT(DATETIME, @HijriDateString, 131);
declare @FormattedOuput varchar(255) = convert(nvarchar(255), @HijriDate, 131);
select @HijriDateString as [original string], @HijriDate as [parsed datetime2], @FormattedOuput as [formatted output]
-- original string parsed datetime2 formatted output
-- 02/02/1435 2013-12-05 00:00:00.0000000 2/02/1435 12:00:00.0000000AM
然後,當你提出的日期到用戶將它們轉換,格式化回來了作爲回曆。
通過保持它在DATETIME2,你可以做適當的日期計算,使用像
MyDateColumn < getutcdate()
事情做正確的比較。
@ Dotctor的問題編輯白天的正常分組不正常嗎?還有每一個機會,我失去了一些東西的關鍵,所以我很樂意學習
begin tran
create table #hijriDateExample (id int, d datetime2)
insert into #hijriDateExample values
(1, CONVERT(DATETIME, '02/02/1435 01:00' , 131)),
(2, CONVERT(DATETIME, '02/02/1435 22:00' , 131)),
(3, CONVERT(DATETIME, '03/02/1435 04:00' , 131))
select dateadd(DAY,0, datediff(day,0, d)), count(*) as [numberOfDays] from #hijriDateExample
group by dateadd(DAY,0, datediff(day,0, d))
-- 2013-12-05 00:00:00.000 2
-- 2013-12-06 00:00:00.000 1
rollback tran
@dotctor這是非常大的.. –
你是什麼非常大的呢? – dotctor
@dotctor char(17)是17byte,但DateTime2(n)是8byte。我也想在這個列上使用sql中的'datepart'函數。 –