線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3278
推到 Plurk!
推到 Facebook!

SQL 語法 : where INDATE=指定日期 請問怎樣下

答題得分者是:deity
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-07-20 23:26:54 IP:203.203.xxx.xxx 未訂閱
我想用 Delphi 內建的 SQL Explorer 來測試 SQL 語法 使用 Delphi DEMO : DBDEMOS 中的 orders.db select * from orders where SaleDate='1998/5/11' 結果有 type 方面的問題 請問, 在 SQL Explorer 中, 日期比較怎樣處理
yinjiajun
一般會員


發表:4
回覆:14
積分:3
註冊:2005-02-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-07-20 23:41:52 IP:218.94.xxx.xxx 未訂閱
SELECT * FROM orders WHERE SaleDate=CAST('1998-05-11' AS DATE) 使用CAST来转换数据格式应该可以了。
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-07-20 23:42:22 IP:211.22.xxx.xxx 未訂閱
select * from orders where SaleDate=1998/5/11 //如果欄位是日期型態,去掉單引號看看~ ------------------- 學,然後知不足。
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-07-21 00:05:15 IP:203.203.xxx.xxx 未訂閱
yinjiajun , 語法沒錯, 但是得不到資料, SaleDate 的 type 是 TIMESTAMP , select * from orders , 列出所有資料, SaleDate 顯示 1988/5/11 Stallion , 出現語法錯誤的訊息 Delphi5 的 orders.db 在 C:\Program Files\Common Files\Borland Shared\Data
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-07-21 10:07:02 IP:218.15.xxx.xxx 未訂閱
cwc65536您好: 因:timestamp最有用的特性就是他会在记录发生任何更改(即使修改后又回滚事务恢复原数据), 时自动更新,并且更新出来的值在数据库中是不会重复的,也就是可以与GUID类型字段相比,但timestamp除了能自动更新外,更只占8字节,比GUID好用且有效率 根据SQL Server2000線上叢書 timestamp timestamp 是顯露自動產生的二位元數字的資料型別,它保證在資料庫中為唯一的。timestamp 通常用於版本戳記資料表資料列的機制。儲存大小為 8 位元組。    備註 Transact-SQL timestamp 資料型別不同於 SQL-92 標準所定義的 timestamp 資料型別。 但 SQL-92 timestamp 資料型別卻相等於 Transact-SQL datetime 資料型別。     未來版本的 Microsoft® SQL Server™ 將會修改 Transact-SQL timestamp 資料型別的功能,使其與標準定義的功能一致。 屆時,現有的 timestamp 資料型別將會被 rowversion 資料型別所取代。    Microsoft® SQL Server™ 2000 引進了 timestamp 資料型別的同義資料表 rowversion。 請盡可能在 DDL 陳述式中以 rowversion 取代 timestamp。rowversion 是歸屬於資料型別同義資料表的功能。若需詳細資訊,請參閱資料型別優先順序。    在 CREATE TABLE 或 ALTER TABLE 陳述式中,您無須對 timestamp 資料型別套用資料行名稱:    CREATE TABLE ExampleTable (PriKey int PRIMARY KEY, timestamp)    如果您不套用資料行名稱,SQL Server 會產生 timestamp 的資料行名稱。 而 rowversion 資料型別同義資料表則不允許此行為。 當您指定 rowversion 時,必須套用資料行名稱。    一個資料表只可以有一個 timestamp 資料行。每次插入或更新包含 timestamp 資料行的資料列時,便會更新 timestamp 資料行中的值。此特性使 timestamp 資料行不適合作為索引鍵,尤其是作為主索引鍵。任何對資料列的更新都會變更 timestamp 值,然後變更索引鍵值。如果此資料行是在主索引鍵中,舊的索引鍵不再有效,參照舊索引鍵的外部索引鍵也不再有效。如果資料表被資料指標參照,所有的更新會變更資料指標中資料列的位置。如果資料行是在索引鍵值中,所有對此資料列的更新也會產生索引的更新。    不可設定空值的 timestamp 資料行在意義上等於 binary(8) 資料行。可設定空值的 timestamp 資料行在意義上等於 varbinary(8) 資料行。 所以timestamp并非字面意思上的时间!而要和字串做比较,您可试试如下写法: SELECT * FROM orders WHERE  SaleDate = CAST('1998/5/11 12:00:00' AS TIMESTAMP )          ============================ 为什么经过多年以后,得失的过程如此冷漠 ============================
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-07-22 01:14:32 IP:203.203.xxx.xxx 未訂閱
deity 您好 : 沒想到 timestamp 有這麼多故事, 其實我只是要問一般的日期欄位的SQL過濾語法,就隨便從 Delphi5 的Demo Data 中撈一個來當例子. 勞您費心寫這麼詳盡的解說, 真是太感謝了. 但是,我回頭去看 orders.db 中, 他卻有兩個type為 timestamp 的field ,一是 SaleDate, 另一是 ShipDate, 與兄所言有些許出入, 也許您說的:只能存在一個TimeStamp是 MS-SQL 的限制吧 ! 而且,select * from orders where SaleDate = CAST('1998/5/11 12:00:00' AS TIMESTAMP ) 無法得到資料, 我看SaleDate內容是有 1998/5/11 所以把SQL改成 : select * from orders where SaleDate = CAST('1998/5/11' AS TIMESTAMP ) 結果還是一樣 我使用的是 Delphi5 內建的 Tool , SQL Explorer , 開啟 orders.db, 應該是 paradox 的檔案吧 ! 其實 DateTime , Date , Time 都同樣讓我無法在 SQL Explorer 隨心所欲的下指令, 也許那位高手可以點撥一下
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-07-22 11:43:54 IP:219.129.xxx.xxx 未訂閱
cwc65536您好: 试试如下: select * from orders where saledate ='4/12/1988' //日期的标准写法 小弟试了没问题 您试试看 <> ============================ 为什么经过多年以后,得失的过程如此冷漠 ============================
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-07-22 12:21:24 IP:203.203.xxx.xxx 未訂閱
感恩 deity 等各位大大的熱心: 真的是江湖一點訣, 原來只是排列順序的問題 我看 select * from orders 的結果, saledate 長相是 1988/5/11 但是用 where 卻要改成 select * from orders saledate='5/11/1988' 天呀 ! 請教 '5/11/1988' 是標準的嗎 ? 不會因為 windows 系統的時區,格式改變吧! 若包含時間, 標準是 24hr 制, 還是要加 AM,PM ? 好像問題很多,我想應該也是困擾著很多初學如我者, 我覺得, 既然是下 SQL 語法, 當然就用標準語法, 顯示或輸入資料也是儘量配合SQL標準比較好 廢話一堆,讓各位前輩見笑了! 還望,不吝賜教 , 3Q
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-07-22 15:36:02 IP:219.129.xxx.xxx 未訂閱
cwc65536您好: 其实这只是BDE 的问题,参考下面的说明, BDE中的日期格式默认为mm/dd/yy,所以要输入必须采用: select * from orders where saledate ='4/12/1988' 才行!    此格式可在控制面版中的BDE Administrator中(或是开始菜单)的 config=>system=>date中设定 其中FOURDIGITYEAR  是否以四位表示年份(默认为两位) LEADINGZEROD  是否表示用两位表示日期,不足两位补0  (默认为false:一位) LEADINGZEROM  是否表示用两位表示月份,不足两位补0  (默认为false:一位) mode: 0表示mdy(即月日年),1表示DMY,2表示YMD SEPARATOR:表示分隔符默认为/ YEARBIASED:是否在两位表示年份加1900,如果为TRUE时96表示为1996,否则表示为0096年 记住一定要加单引号或双引号将日期引上 具体说明见下: Parameter  Description SEPARATOR  Character used to separate the month, day, and year components of a date value; such as, the "/" in "12/31/96." The default is the character normally used in the country selected in the Windows Control Panel when any BDE application is installed. MODE  Controls the order of the month, day, and year components and can be 0 (for MDY), 1 (for DMY), or 2 (YMD). The default is the order normally used in the country selected in the Windows Control Panel when any BDE application is installed. FOURDIGITYEAR  Specifies the number of digits for the year value (four or two). If TRUE, years are expressed in four digits (such as, 1996). If FALSE, the default, years have two digits (96). YEARBIASED  Tells the BDE application whether or not it should add 1900 to years entered as two digits. For example, if TRUE and you enter "7/21/96," the BDE application interprets your value as "7/21/1996", otherwise, the date is interpreted as entered (in this case, "7/21/0096"). Default: TRUE LEADINGZEROM  Specifies whether or not single digit month values have a leading zero. For example, if you enter "1/1/80" and this is set to TRUE, the BDE application interprets the date as "01/1/80." If FALSE, the value is "1/1/80." Default: FALSE LEADINGZEROD  Controls whether or not single digit day values have a leading zero. For example, if you enter "1/1/80" and this is set to TRUE, the BDE application interprets the value as "1/01/80." If FALSE, your date is "1/1/80." Default: FALSE    hope helpful for you!Good Luck!     ============================ 为什么经过多年以后,得失的过程如此冷漠 ============================
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-07-22 18:30:28 IP:203.203.xxx.xxx 未訂閱
deity 您好: 好厲害,知道這麼多. 我的BDE config 如下 FOURDIGITYEAR = false LEADINGZEROD=TRUE LEADINGZEROM=TRUE MODE=0 SEPARATOR=/ YEARBIASED=TRUE 結果 : select * from orders where saledate='5/11/1988' 可以 變成 : select * from orders where saledate='05/11/1988' 也可以 其實,我用的是蠻冷門的資料庫 DBISAM V4.14 , 不知道要在哪裏設定, 原以為它支援 SQL-92 , 所以, 才利用一個大家都能用的 paradox 來發問, 結果, 還是受到 windows or BDE 影響, 萬一 user 亂動 windows 設定, 我們的系統就掛了 而且,最壞的消息是 : select * from orders where saledate='5/11/1988' 居然不被 dbisam 所接受 天呀 ! 沒標準作法嗎 ? 真難搞 ! 我看之前的討論文章,有人提到: date,time,datetime,datestamp 都不用,乾脆全都用 string , 真絕呀 ! 真的要這樣嗎 ? 各位老前輩,有沒有忠告可以給我 !?
系統時間:2024-05-20 4:18:06
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!