VB.net SQL statement to query 1 yr old Timestamp

谁说胖子不能爱 提交于 2019-12-08 12:07:03

问题


I want to display in my gridview all data whose timestamp is not older than 1 years from current date.

My timestamp is formatted like so: 20110125-071830 or yyyymmdd-hhmmss

I have tried to reference :

Retrieve rows less than a day old:

Select *  from table_name WHERE DATE(timestampVal) > DATE(NOW() - INTERVAL 1 YEAR);

but got missing expression flag

I also tried various others; however, my timestampVal is different simply by how it is formatted.

Like for example: https://community.oracle.com/thread/2207956

With coding: Select * from table_name WHERE timestampVal < sysdate - interval '1' year ;

but get error: literal does not match format string which means sysdate can't read how mine is formatted.

How do I query my timestamp to pull all that are a year or less old?

FYI: timestampVal is string type [varchar]


回答1:


It looks like the following should work:

Select *
  from table_name
  WHERE TO_DATE(timestampVal, 'YYYYMMDD-HH24MISS') > sysdate - interval '1' year;

Also note that I reversed the comparison: you indicated that you want rows where timestampVal is not older than 1 year ago - so timestampVal should be greater (newer) than current time minus one year.

Give that a try.

Best of luck.



来源:https://stackoverflow.com/questions/33586341/vb-net-sql-statement-to-query-1-yr-old-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!