這個(gè),你只能把這個(gè)讀取出來(lái)的數(shù)據(jù)當(dāng)字符串處理了!!

創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站與策劃設(shè)計(jì),茂南網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:茂南等地區(qū)。茂南做網(wǎng)站價(jià)格咨詢(xún):18980820575
用空格去分解這個(gè)字段,保存在數(shù)組里,然后提取第一個(gè)為月份,第二個(gè)為日期,第三個(gè)為年份,第四個(gè)為小時(shí)和分鐘,已經(jīng)上下午
$time
=
$rs["lastlogin"];
$times
=
explode("
",$time);
$month
=
$times[0];
$day
=
$times[1];
$year
=
$times[2];
$hour
=
substr($times[3],0,2);
$minute
=
substr($times[3],3,2);
if(substr($times[3],5,1)=='p')
{
$hour
=
intval($hour);
if($hour12)
{
$hour
+=
12;
}
}
$realtime
=
$year."-".$month."-"."day"."
".$hour.":".$minute.":00";
echo
$realtime;
sql
server:
取年:
year(時(shí)間)
或者datepart(year,時(shí)間)
取月:
month(時(shí)間)或者datepart(month,時(shí)間)
oracle:
取年:
extract(year
from
時(shí)間)
或者
to_char(時(shí)間,'yyyy')
取月:
extract(month
from
時(shí)間)
或者
to_char(時(shí)間,'mm')
當(dāng)然,上述前提都是你的時(shí)間是時(shí)間類(lèi)型的,比如sql
server中是datetime類(lèi)型,oracle中是date類(lèi)型的.
如果時(shí)間本來(lái)就是用字符串形式存儲(chǔ)的,那直接根據(jù)格式用字符串函數(shù)處理就可以了.
可以用substring截取,也可以轉(zhuǎn)換成datetime然后用year、month、day三個(gè)函數(shù)計(jì)算
以標(biāo)準(zhǔn)日期格式2012-12-19
10:50:02.000為例
substring(col,1,4)=year
substring(col,6,2)=month
substring(col,9,2)=day
year(convert(datetime,col))、month(convert(datetime,col))、day(convert(datetime,col))
select convert(varchar(7),date_colName,121) from table
通過(guò)convert來(lái)對(duì)日期進(jìn)行格式化,只返回年月部分。
CONVERT() 函數(shù)可以用不同的格式顯示日期/時(shí)間數(shù)據(jù)。
基本語(yǔ)法如下:
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length) 規(guī)定目標(biāo)數(shù)據(jù)類(lèi)型(帶有可選的長(zhǎng)度)。data_to_be_converted 含有需要轉(zhuǎn)換的值。style 規(guī)定日期/時(shí)間的輸出格式。
如上121的格式對(duì)應(yīng)為:yyyy-mm-dd hh:mi:ss.mmm(24h)
而上面定義的varchar(7),因此返回7個(gè)長(zhǎng)度,即yyyy-mm
declare?@time?datetime
set?@time='2016/11/18?09:10:11.123'
select?convert(char(10),@time,120)
還有其他很多方法,例如截?cái)嘧址?/p>
sql取時(shí)間字段中年月日的值的步驟如下:
我們需要準(zhǔn)備的材料分別是:電腦、sql查詢(xún)器。
1、首先,打開(kāi)sql查詢(xún)器,連接上相應(yīng)的sql表。
2、點(diǎn)擊“查詢(xún)”按鈕,輸入:SELECT date_format(add_time, '%Y') as year, date_format(add_time, '%m') as month, date_format(add_time, '%d') as day FROM test;。
3、點(diǎn)擊“運(yùn)行”按鈕,時(shí)間字段的年月日就被取出了。
新聞標(biāo)題:sqlserver截取年,sqlserver截取年月日
標(biāo)題鏈接:http://chinadenli.net/article13/dsspggs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、建站公司、、網(wǎng)站設(shè)計(jì)、云服務(wù)器、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)