Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQLÏÂÈÕÆÚº¯ÊýʹÓÃ

ÕâÁ½Ììдһ¸öºÍͳ¼ÆÊý¾ÝÓйصĴ洢¹ý³Ì£¬ÀïÃæÒªÀûÓÃÈÕÆÚ½øÐÐһЩ¼ÆËãºÍÅжϣ¬Ò²×ÔȻҪÀûÓÃSQLµÄһЩÈÕÆÚÏà¹ØµÄº¯Êý¡£ÕâÀïÂÔ¼Çһϣ¬µ±ÊǸ´Ï°Ò»ÏÂSQL¡£
ÀûÓÃSQL½Å±¾ÄÚÖõļ¸¸öº¯Êý£¬ÎÒÃÇÄÜÁé»îµÄ¶ÔÈÕÆÚ½øÐмÆËãºÍ±È½Ï¡£³£Óõļ¸¸öº¯Êý£ºGETDATE(),DATEDIFF(),DATEADD()
GETDATE() µ±È»¹ËÃû˼Ò壬µÃµ½µ±Ç°µÄÈÕÆÚ£¬·µ»ØÀàÐÍÊÇDateTimeÀàÐÍ¡£
DATEDIFF ( datepart , startdate , enddate ) ÓÃÓÚÅжÏÔÚÁ½¸öÈÕÆÚÖ®¼ä´æÔÚµÄÖ¸¶¨Ê±¼ä¼ä¸ôµÄÊýÄ¿¡£
µÚÒ»¸ö²ÎÊýÊÇÖ¸¶¨Ê±¼ä¼ä¸ôµÄÀàÐÍ,ÀýÈçmm(ÔÂ),dd(Ìì)£¬yy(Äê),ms(ºÁÃ룩,ss(Ã룩£¬²»Í¬µÄ¼ä¸ôÀàÐÍ£¬·µ»ØµÄ½á¹ûÒ²²»Ò»Ñù¡£
DATEADD (datepart , number, date ) ÓÃÓÚÈÕÆÚÔËËãµÄº¯Êý£¬½«´«ÈëµÄÈÕÆÚ£¬¼ÓÉÏÖ¸¶¨Ê±¼ä¼ä¸ôÊýÄ¿µÄÈÕÆÚ¡£
ÀýÈ磬¼ÆËãµÃµ½±¾ÄêµÄµÚÒ»Ì죺
Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
ÎÒÃÇÀ´·ÖÎöÏÂÕâ¸öSQLÓï¾ä¾Í¿ÉÒÔÖªµÀ£¬Ê×ÏÈ´Ó×îÀïÃæµÄgetdate()¿ªÊ¼£¬getdate()µÃµ½µ±Ç°ÈÕÆÚºÍʱ¼ä£¬Íâ²ãµÄdatediff£¬¼ÆË㵱ǰÈÕÆÚºÍ1900-01-01 00:00:00Ö®¼äµÄʱ¼ä¼ä¸ô£¬·µ»Øµ¥Î»ÊÇÒÔÄêÀ´Í³¼ÆµÄ£¬Èç¹ûÎÒÃÇ·Ö¿ª¿´DATEDIFF(yy,0,getdate()),0)µÄ½á¹û£¬·µ»Ø¾ÍÊÇ:109¡£·µ»ØµÄ109£¬´«µÝ¸ø×îÍâ²ãµÄDATEADDº¯Êý£¬½«1900-01-01 00£º00£º00¼ÓÉÏ109Ä꣬µÃµ½µÄ½á¹û£¬×ÔÈ»¾ÍÊÇ2009-01-01 00:00:00ÁË£¬Ò²¼´±¾ÄêµÄµÚÒ»Ìì¡£
ͬÑù£¬Áé»îµÄÀûÓÃÕ⼸¸öº¯ÊýµÄ×éºÏ£¬ÎÒÃÇ¿ÉÒԵõ½²»Í¬µÄ½á¹û£º
µÃµ½µ±ÔµĵÚÒ»Ì죺Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
µÃµ½µ±Ç°¼¾¶ÈµÄµÚÒ»Ì죺Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
µÃµ½µ±ÌìµÄÆðʼʱ¼ä£º Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
µÃµ½ÉϸöÔÂ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
µÃµ½ÉϸöÔµĵÚÒ»Ì죺 Select DATEADD(m,-1,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
ÆäÔ­Àí¾ÍÊǵõ½µ±ÔµÚÒ»Ì죬ÔÙ¼õÈ¥ÈýºÁÃë(SQLµÄʱ¼äÒÔ3ºÁÃëΪһ¸öµ¥Î»)£¬ÕâÑùÒÔµ±Ç°Îª2Ô£¬µÃµ½µÄ½á¹û¾ÍÊÇ£º2009-01-31 23:59:59.997
µÃµ½È¥ÄêµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
µÃµ½±¾ÔµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
µÃµ½±¾ÄêµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
µÃµ½±¾ÔµĵÚÒ»¸öÐÇÆÚÒ»£º Select DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd,6-datepart(day,getdate()),g


Ïà¹ØÎĵµ£º

sql 2000MMC ²»ÄÜ´ò¿ªÎļþ

 MMC ²»ÄÜ´ò¿ªÎļþ C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC¡£¿ÉÒÔÔ­ÒòÊÇÎļþ²»´æÔÚ£¬²»ÊÇÒ»¸öMMC¿ØÖÆ̨£¬»òÕßÓúóÀ´MMC°æ±¾´´½¨£¬Ò²ÐíҲûÓзÃÎÊ´ËÎļþµÄ×㹻ȨÏÞ¡£
½â¾ö·½·¨£º
ÖØд´½¨´ËÎļþ£¬ÔËÐжԻ°¿òÖÐÊäÈë:mmc
1) ¿ØÖÆ̨-->Ìí¼Ó/ɾ³ý¹ÜÀíµ¥Ôª-->Ìí¼ ......

ÔÚSQLÖÐSelectÓï¾äµÄÖ´ÐÐ˳Ðò

ÔÚSQLÖÐSelectÓï¾äµÄÖ´ÐÐ˳ÐòÈçÏ£º
(1)from×Ӿ䣬×é×°À´×Ô²»Í¬Êý¾ÝÔ´µÄÊý¾Ý
(2)WHERE×Ӿ䣬¸øÓèÖ¸¶¨Ìõ¼þ¶Ô¼Ç¼½øÐÐɸѡ
(3)GROUP BY×Ӿ䣬½«Êý¾Ý»®·ÖΪ¶à¸ö×é
(4)ʹÓþۼ¯º¯Êý½øÐмÆËã
(5)ʹÓÃHAVING×Ó¾äɸѡ·Ö×é
(6)¼ÆËãËùÓбí´ïʽ
(7)ʹÓÃORDER BY¶Ô½á¹û½øÐÐÅÅÐò ......

SQL Server ÈÕÆÚ²Ù×÷È«½Ó´¥

DECLARE @dt datetime
SET @dt=GETDATE()
DECLARE @number int
SET @number=3
--1£®Ö¸¶¨ÈÕÆÚ¸ÃÄêµÄµÚÒ»Ìì»ò×îºóÒ»Ìì
--A. ÄêµÄµÚÒ»Ìì
SELECT CONVERT(char(5),@dt,120)+'1-1'
--B. ÄêµÄ×îºóÒ»Ìì
SELECT CONVERT(char(5),@dt,120)+'12-31'
--2£®Ö¸¶¨ÈÕÆÚËùÔÚ¼¾¶ÈµÄµÚÒ»Ìì»ò×îºóÒ»Ìì
--A. ¼¾¶ÈµÄµÚÒ»Ìì
SELECT CON ......

±ê×¼³£ÓÃsqlÓï¾ä Ò»

 SQL ÊÇÒ»ÃÅ ANSI µÄ±ê×¼¼ÆËã»úÓïÑÔ£¬ÓÃÀ´·ÃÎʺͲÙ×÷Êý¾Ý¿âϵͳ¡£SQL Óï¾äÓÃÓÚÈ¡»ØºÍ¸üÐÂÊý¾Ý¿âÖеÄÊý¾Ý¡£SQL ¿ÉÓëÊý¾Ý¿â³ÌÐòЭͬ¹¤×÷£¬±ÈÈç MS Access¡¢DB2¡¢Informix¡¢MS SQL Server¡¢Oracle¡¢Sybase ÒÔ¼°ÆäËûÊý¾Ý¿âϵͳ¡£
²»ÐÒµØÊÇ£¬´æÔÚןܶ಻ͬ°æ±¾µÄ SQL ÓïÑÔ£¬µ«ÊÇΪÁËÓë ANSI ±ê×¼Ïà¼æÈÝ£¬ËüÃDZØÐëÒÔÏàËÆµÄ ......

SQLÖÐÒÔÈÕÆÚΪÌõ¼þͳ¼Æ·½·¨

 /*ͳ¼ÆÿÌìÊý¾Ý×ÜÁ¿ÈýÖÖ·½·¨£º
        select convert(char(10),happentime ,120) as date ,count(1) from table1
        group by convert(char(10),happentime ,120) order by date desc
       s ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ