question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Encountered unexpected token: "DATE_SUB" S_IDENTIFIER

See original GitHub issue

Actual Behavior

public static void main(String[] args) throws JSQLParserException {
    String sql = "-- type 1 stands for sth1, 2 stands for sth2...\n" +
                 "SELECT count(1)\n" +
                 "FROM table_name\n" +
                 "WHERE 1 = 1\n" +
                 "AND uid = 1\n" +
                 "AND type IN (1, 2, 3)\n" +
                 "AND time >= TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL 2 DAY),'00:00:00')\n" +
                 "AND time < TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL (2 - 1) DAY),'00:00:00')";

    Statement parse = new CCJSqlParserManager().parse(new StringReader(sql));
    System.out.println(parse);
}

above code throws and exception:

Exception in thread "main" net.sf.jsqlparser.JSQLParserException
	at net.sf.jsqlparser.parser.CCJSqlParserManager.parse(CCJSqlParserManager.java:40)
Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "DATE_SUB" <S_IDENTIFIER>
    at line 8, column 22.

Was expecting one of:

    "+"

	at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:18597)
	at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:18447)
	at net.sf.jsqlparser.parser.CCJSqlParser.RegularCondition(CCJSqlParser.java:6278)
	at net.sf.jsqlparser.parser.CCJSqlParser.Condition(CCJSqlParser.java:5931)
	at net.sf.jsqlparser.parser.CCJSqlParser.AndExpression(CCJSqlParser.java:5893)
	at net.sf.jsqlparser.parser.CCJSqlParser.OrExpression(CCJSqlParser.java:5818)
	at net.sf.jsqlparser.parser.CCJSqlParser.Expression(CCJSqlParser.java:5789)
	at net.sf.jsqlparser.parser.CCJSqlParser.WhereClause(CCJSqlParser.java:5146)
	at net.sf.jsqlparser.parser.CCJSqlParser.PlainSelect(CCJSqlParser.java:3082)
	at net.sf.jsqlparser.parser.CCJSqlParser.SetOperationList(CCJSqlParser.java:3235)
	at net.sf.jsqlparser.parser.CCJSqlParser.SelectBody(CCJSqlParser.java:2946)
	at net.sf.jsqlparser.parser.CCJSqlParser.Select(CCJSqlParser.java:2939)
	at net.sf.jsqlparser.parser.CCJSqlParser.SingleStatement(CCJSqlParser.java:130)
	at net.sf.jsqlparser.parser.CCJSqlParser.Statement(CCJSqlParser.java:81)
	at net.sf.jsqlparser.parser.CCJSqlParserManager.parse(CCJSqlParserManager.java:38)
	... 1 more

Expected Behavior

hope that can support date_add / date_sub method

Steps to Reproduce the Problem

  1. import jsqlparse-1.3
  2. run java code in IntelliJ IDEA
  3. exception happened

Specifications

  • Version: jsqlparser-1.3
  • Platform: OS X EI Capitan(10.11.6), jdk8, mysql
  • Subsystem:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
shiyifancommented, Dec 4, 2018

i meet this with “date_add”…

0reactions
manticore-projectscommented, Nov 22, 2022

Parses fine with JSQLParser 4.6 Snapshot:

-- type 1 stands for sth1, 2 stands for sth2...
SELECT Count( 1 )
FROM table_name
WHERE 1 = 1
    AND uid = 1
    AND type IN ( 1, 2, 3 )
    AND time >= Timestamp( Date_Sub( Curdate(), INTERVAL 2 DAY ), '00:00:00' )
    AND time < Timestamp( Date_Sub( Curdate(), INTERVAL(2 - 1) DAY ), '00:00:00' )
;

c Closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error in Google BigQuery <EOF> - sql - Stack Overflow
I need help on a query error: "Encountered " "WITH" "with "" at line 1, column 1. Was expecting: EOF". WITH is supported...
Read more >
Solved: SQL Query Failed with Cloudera Hive JDBC driver bu...
In this query I am using ':custId' to fetch the customer id from property (coming from test data file in soap ui). Please...
Read more >
MyBatisPlus解析WEEK关键字出现异常的踩坑记录 - CSDN博客
ParseException: Encountered unexpected token: "CURDATE" ... SELECT xxx FROM (SELECT yyy FROM a WHERE create_time >= date_sub(CURDATE(), ...
Read more >
Unexpected Token on setting Datetime Variable
Please review the snippit below: dateTime dt = date.today()-90; public String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = ...
Read more >
pyspark.sql module - Apache Spark
LongType column named id, containing elements in a range from start to end ... When a length of parsed CSV tokens is shorter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found