1
在學校網站中,我想讓管理員在他們出生時根據日期範圍對學生進行過濾。在我tblStudent日期存儲爲字符串,所以我不能使用:在數據庫表tblStudent
在SQL Server 2012中轉換字符串日期到日期類型
SELECT ts.Name from tblStudent ts WHERE ts.BirthDay>'1367/01/31' AND ts.BirthDay<'1377/01/31'
我已保存日期(賈拉利格式)。我需要根據日期進行比較。所以我需要將日期字符串轉換爲sql server中的日期類型。爲了這個目的,我用:
SELECT convert(date,tblStudent.BirthDay) from tblStudent
然而,27周後的結果與下面的錯誤
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
我在tblStudent表中的下列日期字符串停止。
1379/09/01
1375/04/20
1378/03/02
1378/03/21
1378/04/18
1378/04/18
1378/05/05
1375/04/20
1379/01/03
1378/03/01
1370/09/09
1378/03/22
1375/09/15
1379/09/01
1379/09/10
1375/04/08
1375/05/06
1370/09/09
1379/10/10
1375/04/10
1375/11/01
1375/04/04
1375/08/11
1375/05/05
1376/09/19
1375/12/12
1376/01/13
1375/15/10
1375/04/14
1375/04/04
1375/05/14
1374/11/11
1375/05/30
1375/05/14
1377/12/13
1377/02/31
1377/12/14
1377/01/13
1375/05/31
1377/11/05
1377/07/05
1375/05/31
1377/03/01
1377/04/01
1377/05/02
1377/05/04
1377/03/03
1377/01/14
1377/05/30
1377/04/31
1375/05/30
1376/06/12
1375/12/10
1377/08/14
1377/03/04
1375/04/08
1375/07/18
1375/08/09
1375/09/12
1375/11/12
1376/12/12
1375/01/02
1375/05/09
1375/04/09
1376/01/01
1375/01/30
1377/04/04
1375/05/23
1375/05/01
1377/02/01
1367/12/05
1375/05/31
1373/03/29
1373/03/03
1375/05/05
有沒有辦法將這些字符串日期轉換爲日期類型,然後將它們與某些查詢進行比較?例如,這樣的查詢可以是:
SELECT ts.Name from tblStudent ts where ts.BirthDay>'1375/05/31'
很酷,這適用於數據庫中的數據。我想知道我可以使用什麼C#函數將用戶輸入的'yyyy/mm/dd'日期格式轉換爲yyyymmdd int – JasonStack 2014-09-04 12:11:07
@misaq檢查我編輯的解決方案。 – 2014-09-04 12:17:56