天天看點

mysql substr 位元組_mysql中的substr()函數

mysql中的substr()函數和hibernate的substr()參數都一樣,就是含義有所不同。

用法:

substr(string string,num start,num length);

string為字元串;

start為起始位置;

length為長度。

差別:

mysql中的start是從1開始的,而hibernate中的start是從0開始的。

最常用在時間時間搜尋上如

2016-09-01 19:02:19到2016-09-20 19:02:19

想搜尋當天的如1号到1号的就要寫成

if (sdate != null && !"".equals(sdate)) {

sql += " and substr(" + timetype + ",1,10)>='" + sdate + "'";

}

if (edate != null && !"".equals(edate)) {

sql += " and substr(" + timetype + ",1,10)<='" + edate + "'";

}

mysql substr 位元組_mysql中的substr()函數

這裡timetype 代表按什麼方式搜并且取值為1到10位sdate為開始時間edate為結束時間資料庫中的時間為2016-09-01 19:02:19我們現在隻需要前面10位如果不這樣你無法搜尋當天的資料如14号當天的資料