1、有如下sql語句:
select count(*) from jcjg where ( date(substr(jcsj,1,4)||"-"||substr(jcsj,5,2)||"-"||substr(jcsj,7,2)) between date("2015-05-29") and date("2015-06-01") );
2、從網上查詢得知,substr是oracle語法中的。如下所示;
a)、substr(new_day,0,6) substr 是從new_day的第1個字符開始截取6位字符並返回
比如new_day = 'abcdefgh' 則返回 'abcdef'
b)、to_char(date,'yyyymm') 是指 將date(日期型)轉換爲字符串類型 格式爲yyyymm
比如date爲2011-12-12 則返回'201112'
c)、to_date(str,'yyyymm') 將str(字符型)轉換爲日期類型 字符的格式爲yyyymm
其中字符必須爲201112 才能正確轉換
d)、如果字符的格式爲yyyy-mm-dd 則字符必須爲'2011-12-12'才能正確轉換
3、在sqlite中好像有點不同:
如:substr(new_day,1,6) substr 是從new_day的第1個字符開始截取6位字符並返回
比如new_day = 'abcdefgh' 則返回 'abcdef'。
4、例子:
從win端打開sqlite數據庫,然後jcjg表的結構如圖所示:
然後執行如下sql語句:
select count(*) from jcjg where ( date(substr(jcsj,1,4)||"-"||substr(jcsj,5,2)||"-"||substr(jcsj,7,2)) between date("2015-05-29") and date("2015-06-01") );
數據庫中顯示的結果爲:
5、如果between後面的時間格式和前面的格式不相同,sql語句執行完後,顯示的結果count(*)列爲0。
如圖所示:
substr(string,start,length)
string - 指定的要截取的字符串。start - 必需,規定在字符串的何處開始。正數 - 在字符串的指定位置開始,負數 - 在從字符串結尾的指定位置開始,0 - 在字符串中的第一個字符處開始。length - 可選,指定要截取的字符串長度,缺省時返回字符表達式的值結束前的全部字符。
例如:select substr('abcdefg',3,4) from dual; 結果是cdef select substr('abcdefg',-3,4) from dual; 結果efg
注意:字符串中的第一個位置始終爲1。以下兩個sql查詢的結果相同:
例如:select substr('abcdefg',0,3) from dual; 結果是abc
select substr('abcdefg',1,3) from dual; 結果是abc
7、與java類中的substring區別:
substring(int beginIndex, int endIndex) :從指定的 beginIndex 處開始,到指定的 endIndex-1處結束,字符串中的第一個位置始終爲0。endIndex,可選,缺省時返回始於指定索引處的字符,一直到此字符串末尾。
例如:"unhappy".substring(2) returns "happy"
"hamburger".substring(3,8) returns "burge"