sql中如何獲取表中datetime型別的指定部分?

時間 2025-03-09 11:45:27

1樓:匿名使用者

select datediff(year,birthday,getdate())as age from a where number='01'

此句是以年份為依據,兩個年份相減而得。

select case

when day(getdate())day(birthday)<0

then datediff(month,birthday,getdate())12-1

when day(getdate())day(birthday)>=0

then datediff(month,birthday,getdate())12

endas age from a where number='01'

此句是以月份為依據。

得到準確的年齡計算:

select case

when datepart(dy, getdate())datepart(dy, birthday)<0

then datediff(year,birthday,getdate())1

when datepart(dy, getdate())datepart(dy, birthday)>=0

then datediff(year,birthday,getdate())

endas age from a where number='01'

2樓:網友

select number,birthday,nl=(datediff(yy,birthday,'2014-06-03'))from a

主要是找函式計算,以上也沒有試,試一下行否。

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

3樓:網友

1、點選「開始」——microsoft sql server 2005」——microsoft sql server management studio」。

2、在開啟的「連線到伺服器」介面,輸入伺服器名稱、資料庫登入名和密碼等資訊。

3、點選「連線」,連線到sql server資料庫。

4、使用convert函式可以將字串轉換為日期型別,從而儲存在日期時間型別的欄位中。點選「新建查詢」,新建乙個sql文字。

6、將日期時間格式轉換為字串也是使用convert函式,格式為convert(字串,日期值,格式型別)。

7、點選「執行」,檢視執行效果。

4樓:網友

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

測試方法如下:

建立表: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())

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

5樓:飛鷹

1、目的是去掉下圖中的回車符號。

2、首先在上方的工具欄中選擇「檔案」。

3、接著選擇「選項」。

4、進入選項介面後,點選「顯示」。

5、然後取消勾選「段落標記」。

6、操作完畢後,點選「確定」。

7、文章中的回車符就被清除了。就可以去掉sql server裡datetime型別裡面的時間部分了。

6樓:網友

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

這樣就可以了。

從sqlserver資料庫中提取日期,並把年月日分別擷取出來

7樓:網友

工具/材料:management studio。

1、首先在桌面上,點選「management studio」圖示。

2、然後在該介面中,點選左上角工具欄裡「新建查詢」按鈕。

4、之後在該介面中,輸入提取日期,並把年月日分別擷取出來的sql語句「select year(getdate())month(getdate())day(getdate())

5、然後在該介面中,點選上方左側的「執行」按鈕。

6、最後在該介面中,顯示分別擷取出來的年月日。

8樓:axure夜話

從sqlserver資料庫中提取日期應該使用,並把年月日分別擷取出來應該使用。

資料庫提供的時間函式。

1:使用year,month,day用來提取年月日。

如:select year(getdate())month(getdate())day(getdate())

2:使用datepart 獲取年月日。

如:select datepart('year',getdate())datepart('month',getdate())datepart('day',getdate())

如果欄位是varchar型別的話,可以先將欄位轉換為日期型別。

使用型別轉換函式convert或者cast

如:cast('2015-07-14' as datetime)

9樓:網友

可以用substring擷取,也可以轉換成datetime然後用year、month、day三個函式計算。

以標準日期格式2012-12-19 為例。

substring(col,1,4)=yearsubstring(col,6,2)=monthsubstring(col,9,2)=dayyear(convert(datetime,col))、month(convert(datetime,col))、day(convert(datetime,col))

10樓:小童鞋_成

思路:先把日期轉換成字元格式,再通過字串操作函式擷取想要的部分,最後拼湊上你要的部分。

比如:a=2009-9-15 0:00:00left(convert(varchar(20),a,120),7)+'-01 0:00:00 '

說明一下,convert這個函式強制把日期格式轉換成varchar型,120是引數,按odbc標準,yyyy-mm-dd hh:mm:ss格式。

以上是思路,你自己修改一下就可以得到你要的東西。

11樓:網友

select convert(varchar,datepart(year,getdate())年。

-'+convert(varchar,datepart(month,getdate())月。

-'+convert(varchar,datepart(day,getdate())日。

select convert(varchar,datepart(year,[日期欄位]))年。

select convert(varchar,datepart(month,[日期欄位]))月。

select convert(varchar,datepart(day,[日期欄位]))日。

sql中如何使datetime型別的資料只顯示日期?

12樓:桖聽

convert(varchar(30),欄位,23)查詢的時候用的。

select convert(varchar(30),欄位,23) from 表。

顯示的格式就是日期。

查詢的話。用 select * from 表 where [date] like '%2010-7-12%'

或者select * from 表 where charindex(convert(varchar(20),'2010-7-12'),[date])

這兩種都可以。

13樓:網友

用datetime一定會出現你這種結果的。

第一是把型別改了,就是樓上說的,我就不說了。

第二就是在顯示這個時間的地方做一下字串的擷取就可以了,有方法可以直接擷取的的。

14樓:

顯示的時候處理一下。

date"]).tostring("yyy-mm-dd");

15樓:晚街故人

資料庫裡datetime的資料型別改為date就可以了。

sql中如何獲得時間的時,分,秒部分?

16樓:

假設時間欄位為orderdate,可以使用如下語句:

select convert(nvarchar(12),orderdate,108) from tablename

108可以得到你要的時間,想要得到毫秒,可以使用114

17樓:溫靈杉

select datepart(hh,datetime型資料)……

其中的「hh」可以轉變成下面任何一種英文!

全寫 簡寫。

year yy, yyyy

quarter qq, q

month mm, m

dayofyear dy, y

day dd, d

week wk, ww

hour hh

minute mi, n

second ss, s

millisecond ms

sql server 裡如何提取datetime裡的時間!

18樓:網友

不知你的 sql server 是哪個版本,我在sql server 2008下可以將datetime資料型別轉換為time型別,這樣表中現有的datetime資料就會只顯示時間部分,如:

alter table 表 alter column 列 time(0);

注意!在執行以上語句之前,務必備份原有列,因為一旦執行以上語句,那麼日期部分將不再保留,也就是說無法通過逆向操作來還原原有資料。

補充,試了一下2005中不支援time型別,目前能想到的方案就是新增一varchar型別列來儲存時間部分,如:

alter table 表 add 新列 varchar(10);

update 表 set 新列=convert(varchar,原列,108)

19樓:賓士

select convert(varchar,時間欄位,108) from 表名;

-以上,希望對你有所幫助。

20樓:我tm不管

update 表 set 日期欄位 left(convert(varchar,日期欄位,120),10)+' '+'09:00:00'

以上,希望對你有所幫助。

c中如何獲取實際執行的sql語句

1 拼接產生sql語句 string sql insert into czyb yhm,mm,qx values txtname.text txtpassword.text cmbpriority.text oledbcommand cmd new oledbcommand sql,conn 這種方...

SQL中如何在表中新增欄位,在資料表中新增一個欄位的SQL語句怎麼寫

我愛數學 alter table tablename1 add alter column fieldname1 fieldtype nfieldwidth nprecision null not null check lexpression1 error cmessagetext1 default ...

SQL中如何查詢A表中的資料有部分存在B表中並顯示出來

四舍 入 1 select from b表 where item no in select 條碼 from a表 2 select from a表,b表 where a表.條碼 b表.item no 擴充套件資料 sql參考語句 刪除表drop table tabname 這是將表連同表中資訊一起刪...