Derby Database with Date Format alfa + numeric - derby

In storing my date in a derby table as "Apr 5 2017" (alpha + numeric) , will the query code here know how to query properly? As opposed to an all numeric format "04 05 2017". In other words, Can the query handle charvar for month sorting?
(PreparedStatement st = con.prepareStatement("select * from TEST5 where startCol >= date1 and endCol <= date2" );
ResultSet rs = st.executeQuery()) {
MainDisplay.jTable1.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
con.close();

No, if you store the date in the db as a string like this the sorting will just be a string compare and the order will not be correct (how is Derby supposed to know this is a date?).
You should define the column type as being a DATE if you want it to hold a date or TIMESTAMP for a date + time.

Related

java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string (Oracle)

I'm trying to run a script through Java code
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-NOV-19', 'DD-MM-RR')\";");
stmt = con.prepareStatement(query.toString());
rs = stmt.executeQuery();
When trying to run the script in Oracle SQL Developer it works fine, but when running my java code it gives an SQLDataException:
java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:863)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3620)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
I can't find whats causing the error, please anyone? Thank you
Assuming that CREATEDATE is a column of the DATE data type then you do not need to use TO_DATE on it (as it is already a DATE), you also do not need the trailing \";.
You could also use a date literal.
So assuming that CREATEDATE always has a mignight time component:
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE = DATE '2019-11-26'");
If your CREATEDATE column has a non-midnight time component then you need to compare on a range (or use TRUNC but that would not allow you to use an index on the column and would require a separate function-based index):
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE >= DATE '2019-11-26' AND CREATEDATE < DATE '2019-11-27'");
I don' think you need TO_DATE for CREATEDATE as it is already a date.
Also, your TO_DATE('26-NOV-19', 'DD-MM-RR') should be TO_DATE('26-NOV-19', 'DD-MON-RR')
Try this:
SELECT
CNAME,
CREATEDATE
FROM
MERGE_DATA_VW
WHERE
TRUNC(CREATEDATE) = TO_DATE('26-NOV-19', 'DD-MON-RR');
Cheers!!
Wrong date format
TO_DATE('26-NOV-19', 'DD-MM-RR')
Please user Correct date
ie 'DD-MON-RR' FOR '26-NOV-19'
You where caluse should look like -
WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-11-19', 'DD-MM-RR')

Oracle Query With Date Parameter Returns No Records on Date Column

I have the following code -
CMD = new OracleCommand("SELECT * FROM BOOKINGSV3 WHERE DT = :DateInQuestion ", Con);
CMD.Parameters.Add("DateInQuestion", OracleDbType.Date).Value = DateTime.Today;
DT is a Date column, I know Oracle stores dates explicitly and if I try manually with '14-AUG-17 00:00:00' I can retrieve the desired results. How can I search by a date using a parameter other than using a string parameter and constructing a string in the format '14-AUG-17 00:00:00'?
If your DT values have non-midnight times then to get all values on a given day you can use a range:
SELECT * FROM BOOKINGSV3 WHERE DT >= :DateInQuestion AND DT < :DateInQuestion + 1
The >= means it will find times from midnight onwards; the < .. + 1 will limit to before midnight on the following day.
One way to do this is to create a string containing the date you want to search and change your query to explicitly create a date using the date format you use in your string.
CMD = new OracleCommand("SELECT * FROM BOOKINGSV3 WHERE DT = to_date(':DateInQuestion','DD-MM-YYYY HH24:MI:SS') ", Con);
This way you control the date format and not some NLS setting.

SAS PROC SQL String Manipulation

Can I convert a string column formatted as "YYYY-MM" from SQL back into a Date that uses text. (ie. January, 2014) as a new column using proc SQL?
Initially my source is 2014-01 and I would like every row to be converted to the respective month and year as shown. I have tried the Format option outside of the proc SQL table build, however I need it as a Macro date afterwards.
Thanks
Here's one option that uses a custom format. First convert to date using INPUT() with the ANYDTDTE. format. Then you can either display the variable with the format or you can convert it to a new character variable with the format applied.
/*Create your own format definition*/
proc format;
picture monyyc_fmt (default=25)
low - high = "%B, %Y" (datatype=date);
run;
data want;
str='2014-12';
date=input(str,anydtdte.);
format date monyyc_fmt.;
want=put(date, monyyc_fmt.);
put (_all_) (=/);
run;
proc print data=want;
run;
Results are:
Obs str date want
1 2014-12 December, 2014 December, 2014
References for the custom date format:
http://www2.sas.com/proceedings/sugi31/243-31.pdf
Not sure what PROC SQL has to do with the issue of converting the value. One way would be to convert your string to date using INPUT() and then use the CATX(), PUT() and YEAR() functions and the MONNAME. format to build the new string.
data _null_;
str='2014-12';
date=input(str,anydtdte.);
format date date9.;
want=catx(', ',put(date,monname.),year(date));
put (_all_) (=/);
run;
You could skip the middleman and just parse the string yourself to get the year number. This might be easier to code inside an SQL statement.
want=catx(', ',put(input(str,anydtdte.),monname.),scan(str,1,'-'));
In SQL, consider the next example:
-- The table holding the result
CREATE TABLE #myTable(stringDate CHAR(7), dateValue DATETIME, newStringDate VARCHAR(20))
-- The values in original format
INSERT INTO #myTable(stringDate) SELECT '2014-01'
INSERT INTO #myTable(stringDate) SELECT '2014-02'
INSERT INTO #myTable(stringDate) SELECT '2014-03'
INSERT INTO #myTable(stringDate) SELECT '2015-01'
INSERT INTO #myTable(stringDate) SELECT '2015-02'
GO
-- All months have a first day, so conveting "2014-01" to date is just removing "-" and adding "01"
UPDATE #myTable SET dateValue = CAST(REPLACE(stringDate, '-', '') + '01' AS DATETIME)
GO
-- When we have the dates, you can tranform it to the format you want
UPDATE #myTable SET newStringDate = DATENAME(MONTH, dateValue) + ', ' + CAST(YEAR(dateValue) AS VARCHAR)
GO
SELECT * FROM #myTable

Hibernate criteria for get records which have year 2012

i need all records which have year entered from search criteria.
for ex:
String year = "2012";
In hibernate it work for
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is in format DD-MM-YY.
Please suggest..
criteria.add(Restrictions.like("createDate", "%2012%", MatchMode.ANYWHERE));
But I would prefer changing your db field to a date format(its string at the moment?) and performing a greater than comparison (instead of like).
like only applies for the String . For the date field , you can use Restrictions.between() to generate between '01/01/2012 00:00:00' and '12/31/2012 23:59:59' for the where clause
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date fromDate = df.parse("1-1-2012 00:00:00");
Date toDate = df.parse("31-12-2012 23:59:59");
criteria.add(Restrictions.between("createDate", fromDate, toDate));
Also , please note that all the properties used in the Criteria API is the Java property name , but not the actual column name.

HQL query to filter date from datetime data

I want to convert datetime format to string format in HQL count.
For example, I have redundant attendance data in each day of month of multiple employee which I need to count and get distinct data for a single day.
select Count(distinct att.AttDate) from AttendanceTable att where att.AttDate between '" + startDate.Date + "' and '" + endDate.Date + "'
but this query counting each and every datetime data because of time value. So I need to convert datetime into string.
HQL allows only certain set of functions.
Try
select count(distinct (
day(att.AttDate) +
31 * month(att.AttDate) +
366 * year(att.AttDate) ))
You could try str() or cast() but the result won't be consistent over different databases.

Resources