如何去掉sql server裡datetime型別裡面的時間部分

時間 2021-09-08 03:20:01

1樓:匿名使用者

分兩種情況,一種是查詢時把時分秒等去掉,另一種是新建一個欄位,只儲存日期。

測試方法如下:

建立表:

create table [test](

[id] [int] null,

[starttime] [datetime]);

插入資料:

insert into test values (1,getdate());

如果查詢時去掉就很簡單,用如下語句:

select id,convert(varchar(10),starttime,120) as starttime from test;

結果截圖:

如果新建一個欄位只儲存日期,可用如下方法:

給表新加一個欄位且預設值為當前系統時間的年月日。

alter table test add startdate varchar(10) default convert(varchar(10),getdate(),120);

此時再給表中新增資料:

insert into test(id,starttime) values (2,getdate());

結果檢測:

這時可以看到新加的資料已有了預設值,就是以年月日的方式顯示。

2樓:匿名使用者

sql server 中一個非常強大的日期格式化函式常用:

select convert(varchar(100), getdate(), 23): 2006-05-16

select convert(varchar(100), getdate(), 0): 05 16 2006 10:57am

select convert(varchar(100), getdate(), 1): 05/16/06

select convert(varchar(100), getdate(), 2): 06.05.16

select convert(varchar(100), getdate(), 3): 16/05/06

select convert(varchar(100), getdate(), 4): 16.05.06

select convert(varchar(100), getdate(), 5): 16-05-06

select convert(varchar(100), getdate(), 6): 16 05 06

select convert(varchar(100), getdate(), 7): 05 16, 06

select convert(varchar(100), getdate(), 8): 10:57:46

select convert(varchar(100), getdate(), 9): 05 16 2006 10:57:46:827am

select convert(varchar(100), getdate(), 10): 05-16-06

select convert(varchar(100), getdate(), 11): 06/05/16

select convert(varchar(100), getdate(), 12): 060516

select convert(varchar(100), getdate(), 13): 16 05 2006 10:57:46:937

select convert(varchar(100), getdate(), 14): 10:57:46:967

select convert(varchar(100), getdate(), 20): 2006-05-16 10:57:47

select convert(varchar(100), getdate(), 21): 2006-05-16 10:57:47.157

select convert(varchar(100), getdate(), 22): 05/16/06 10:57:47 am

select convert(varchar(100), getdate(), 23): 2006-05-16

select convert(varchar(100), getdate(), 24): 10:57:47

select convert(varchar(100), getdate(), 25): 2006-05-16 10:57:47.250

select convert(varchar(100), getdate(), 100): 05 16 2006 10:57am

select convert(varchar(100), getdate(), 101): 05/16/2006

select convert(varchar(100), getdate(), 102): 2006.05.16

select convert(varchar(100), getdate(), 103): 16/05/2006

select convert(varchar(100), getdate(), 104): 16.05.2006

select convert(varchar(100), getdate(), 105): 16-05-2006

select convert(varchar(100), getdate(), 106): 16 05 2006

select convert(varchar(100), getdate(), 107): 05 16, 2006

select convert(varchar(100), getdate(), 108): 10:57:49

select convert(varchar(100), getdate(), 109): 05 16 2006 10:57:49:437am

select convert(varchar(100), getdate(), 110): 05-16-2006

select convert(varchar(100), getdate(), 111): 2006/05/16

select convert(varchar(100), getdate(), 112): 20060516

select convert(varchar(100), getdate(), 113): 16 05 2006 10:57:49:513

select convert(varchar(100), getdate(), 114): 10:57:49:547

select convert(varchar(100), getdate(), 120): 2006-05-16 10:57:49

select convert(varchar(100), getdate(), 121): 2006-05-16 10:57:49.700

select convert(varchar(100), getdate(), 126): 2006-05-16t10:57:49.827

select convert(varchar(100), getdate(), 130): 18 ???? ?????? 1427 10:57:49:907am

select convert(varchar(100), getdate(), 131): 18/04/1427 10:57:49:920am

3樓:匿名使用者

convert(varchar(10),欄位,120)

這樣就可以了

4樓:

convert(varchar(10),clum,120)

sqlserver中datetime型別怎樣把輸出的秒去掉,只取出年月日和幾點幾分?

5樓:匿名使用者

sql convert 用法

convert(data_type,expression[,style])

convert(varchar(10),欄位名,轉換格式)

說明:此樣式一般在時間型別(datetime,smalldatetime)與字串型別(nchar,nvarchar,char,varchar)

相互轉換的時候才用到.

例子:select convert(varchar(30),getdate(),101) now

結果為:

now---------------------------------------

|09/15/2001

style數字在轉換時間時的含義如下:

style(2位表示年份) | style(4位表示年份) | 輸入輸出格式

0 | 100 | mon dd yyyy hh:miam(或pm)

1 | 101 美國 | mm/dd/yy

2 | 102 ansi | yy-mm-dd

3 | 103 英法 | dd/mm/yy

4 | 104 德國 | dd.mm.yy

5 | 105 義大利 | dd-mm-yy

6 | 106 | dd mon yy

7 | 107 | mon dd,yy

8 | 108 | hh:mm:ss

9 | 109 | mon dd yyyy hh:mi:ss:

mmmmam(或pm)

10 | 110 美國 | mm-dd-yy

11 | 111 日本 | yy/mm/dd

12 | 112 iso | yymmdd

13 | 113 歐洲預設值 | dd mon yyyy hh:mi:ss:

mmm(24小時制)

14 | 114 | hh:mi:ss:

mmm(24小時制)

20 | 120 odbc 規範 | yyyy-mm-dd hh:mi:ss(24小時制)

21 | 121 | yyyy-mm-dd hh:mi:ss:

mmm(24小時制)

如何去除dedecms頁面版權,如何去掉dede織夢自帶底部版權Power by DedeCms

1 首先我們來講一下powerby標籤如何呼叫,首先我們進入dedecms的後臺,選擇 系統 系統基本引數 站點設定 2 接著在 版權資訊欄中編寫自己的版權資訊,支援html 寫好之後確定儲存。3 然後我們在 的footer.htm檔案中用標籤進行呼叫。根據自己 的實際情況來呼叫位置。5 接著我們就...

SQL Server裡如何刪除資料庫

工具 材料 以win7電腦 sql server management studio為例。1 首先點選電腦左下方 開始 圖示。2 然後在該介面中,點選 sql server management studio 選項。3 之後在該介面中,點選左側 資料庫 選中 要刪除的資料庫 4 接著在該介面中,右鍵...

sqlserver裡儲存過程怎麼呼叫儲存過程

司馬鑄劍 在sql server資料庫的維護或者web開發中,有時需要在儲存過程或者作業等其他資料庫操作中呼叫其它的儲存過程,下面介紹其呼叫的方法 在sql server資料庫的維護或者web開發中,有時需要在儲存過程或者作業等其他資料庫操作中呼叫其它的儲存過程,下面介紹其呼叫的方法 一 sql s...