sql对日期的处理,一个存储过程示例

IF v_docType = 3 THEN
update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date(brithday,'YYYYMMDD'), ID_NUMBER = idNumber,PROVINCE_NO = provinceId,CITY_NO = cityId,COUNTRY_NO = countryId,
ADDRESS = address,CREATE_USER_ID = addUserId,DOCTOR_ID = addUserId
where MOBILE = phone;

ELSE if v_docType=2 then
update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date(brithday,'YYYYMMDD'), ID_NUMBER = idNumber,PROVINCE_NO = provinceId,CITY_NO = cityId,COUNTRY_NO = countryId,
ADDRESS = address,CREATE_USER_ID = addUserId,DOCTOR_ID = addUserId,COMMUNITY_ID = addUserId
where MOBILE = phone;

else
update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date(brithday,'YYYYMMDD'), ID_NUMBER = idNumber,PROVINCE_NO = provinceId,CITY_NO = cityId,COUNTRY_NO = countryId,
ADDRESS = address,CREATE_USER_ID = addUserId,COMMUNITY_ID = addUserId
where MOBILE = phone;
end if;
END IF;

这里BIRTHDAY 传过来的是'YYYYMMDD'类型的字符串,贰表里面该字段的存储类型是Date,就需要在sql里面进行类型转换,这就是一个转换的语句,可以供其他地方参考!

下面这种情况是取得时候直接在sql里面把date转为vcharge

SELECT A.RECORD_ID,A.PAPER_ID,TO_CHAR(A.CREATE_TIME,'yyyy-mm-dd hh24:mi') CREATE_TIME,B.PAPER_TITLE,v_advice_txt ADVICE_CONTENT,v_wj_score SCORE,v_score_desc SCOREDESC
FROM T_RECORD_PAPER A INNER JOIN T_PAPER B
ON A.PAPER_ID=B.PAPER_ID WHERE A.Record_Id=v_recordId;

这里的TO_CHAR  to_date 都是oracle的内置函数,没试过在mysql中能不能适用,应该是不区分大小写的

再来一点

v_start IN NUMERIC,
v_end IN NUMERIC,

and to_char(a.create_time, 'YYYYMMDD') >= v_start
and to_char(a.create_time, 'YYYYMMDD') <= v_end

这里也是可以的,字符与数值进行运算,v_start的格式是160809,oracle的强大灵活,服了吧!

再来一点

--插入医生用户信息表
insert into T_DOCTOR_INFO(DOCTOR_ID,USER_NAME,MOBILE,EMAIL,SEX,BIRTHDAY,HOSPITAL_ID,DEPT_ID,JOBTITLE_ID,INTRODUCE,SOURCE,DOCTOR_TYPE)
values(v_userId,USER_NAME,MOBILE,EMAIL,SEX,to_date(BIRTHDAY,'yyyy/mm/dd'),HOSPITAL_ID,DEPT_ID,JOBTITLE_ID,INTRODUCE,SOURCE,DOCTOR_TYPE);

这里表中的BIRTHDAY是 date ,贰传过来的参数是yyyy/mm/dd的字符串

还有这个:

INSERT INTO T_FRIEND_GROUP_INFO
(GROUP_ID,GROUP_NAME,GROUP_TYPE,USER_ID,CREATE_TIME)
VALUES
(1,'我的病人',1,v_userId,SYSDATE);

自动获取当前时间SYSDATE

又一个转换: vWTTime:=TO_DATE(v_Time,'yyyy-mm-dd,hh24:mi:ss');

上一篇:Java基础(basis)-----抽象类和接口详解


下一篇:12.21-Android WebService(基于KSOAP2)