SQL Query nvarchar to date - mysql

I am working on SAP HANA Studio and have tried to run SQL command that converts an entire column of field, nvarchar, into one of field, date.
My dates have format: dd-mon-yyyy (i.e '29-Mar-1997') with field nvarchar(11).
I have looked at previous questions and SQL command documentation (for functions like CAST, CONVERT, TO_DATE, STR_TO_DATE) and have not gotten a solution.
Typical errors I get are: Function not recognized, or, Error while parsing Service Date as DATE at function to_date().
Any suggestions?
Thanks
-Diana

Try TO_DATE():
select to_date(col, 'DD-MON-YYYY')

Obviously your database driver/layer in SAP HANA does not support all mySQL functions.
Please connect to your database directly (using command-line or a gui like HeidiSQL) and create a view in your database:
CREATE VIEW view_tablename AS
SELECT STR_TO_DATE(`Service Date`, '%d-%b-%Y') AS ServiceDateDt, * FROM tablename
Then use view_tablename instead of tablename in all your queries - because view_tablename has the additional date field "ServiceDateDt".

Related

Find output of MySQL inbuilt function in CLI using custom parameter

I want to see the working of MySQL functions FROM_UNIXTIME() and UNIX_TIMESTAMP() by providing the parameters to them myself in CLI, something like this:
SELECT UNIX_TIMESTAMP(1459460268);
without having to insert these custom values in a table first and then selecting them to see the output.
Thank you for your time :)
UNIX_TIMESTAMP function accepts a date and you are giving it UNIX TIMESTAMP. You can simply run this in MySQL CLI and get the results:
SELECT UNIX_TIMESTAMP("2016-04-01 03:07:48");
SELECT FROM_UNIXTIME(1459460268);
SELECT UNIX_TIMESTAMP();
The last one will return you the current UNIX TIMESTAMP.
Happy coding!

converting java time to sqldate in query

java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.

Using BIDS to extract data from Informix ODBC source with parameters

As it says in the title, I am trying to extract data from my Informix ODBC source with parameters. I have two parameters that i am trying to pass. Both are DateTime and i am trying to get the current starting date for example 2014-10-10 00:00:00 and the ending date 2014-10-10 23:59:59.
If i do this with a normal query:
SELECT * FROM TABLENAME
WHERE STARTDATETIME BETWEEN '2014-10-10 00:00:00' AND '2014-10-10 23:59:59'
Everything works fine. However, if I try and use the parameters that I have set up:
SELECT * FROM TABLENAME
WHERE STARTDATETIME BETWEEN ? AND ?
I get the following error:
Open Database Connectivity (ODBC) error occurred. state: '07001'.
Native Error Code: -11012. [Informix][Informix ODBC Driver]Wrong number of parameters.
I feel like it has to do with my query, but I have been looking and have found nothing. Would anyone be able to help me out? Thanks!
When you use parameterized query ('?') binding is needed.
Likely you may be using ODBC API SQLBindCol for binding it.
The number of parameter in the query (in this case it is 2)
should be matching with number of bind API calls.
Please check your ODBC code to make sure the binding is correctly done.

Unparseable date error in Pentaho

I am using Pentaho to insert and update a table in Mysql.
Source database being oracle 11g and destination is Mysql database.
The query for getting max syncronization time from oracle is
SELECT
max(SYNC_TIME) AS LST
FROM Abc_ADM.ORA_SYNC_STATS
where SYNC_TIME is of Timestamp(6) datatype in Oracle in format 01-FEB-70 12.00.00.000000000 AM.
when i use this query and run the job i get error-
could not convert string [${LST}] to date using format [yyyy/MM/dd HH:MM:ss:SS] on offset location 0
unparseable date [${LST}]
What is that i am declaring wrong? please help
Pentaho is asking for a date-format like
yyyy/MM/dd HH:MM:ss:SS
But your Oracle-Output is different:
01-FEB-70 12.00.00.000000000 AM
For Pentaho its a string, no date at all.
It should work by telling Pentaho the Date-Format:
dd-MMM-yy HH.mm.ss
Do this in an input-Step
or by using a select-values ("Meta-data") step after your input
Important:
Type should be "Date" and Format: dd-MMM-yy HH.mm.ss
I can't post screenshots where you could have seen that it works for me.
T [1]: http://i.stack.imgur.com/1AuPW.jpg

MySQL Date and Time functions don't exist

I have installed WampServer 2.0 with MySQL 5.1.33.
I can do Numeric and String functions like
SELECT ABS(-2)orSELECT LOWER('ASD')
but with Date and Time Functions such as
SELECT CURDATE()orSELECT NOW()
I get
Error : no such function: CURDATE
Am I doing something wrong, is there anything I need to install?
Any help about where to start investigating?
There is no error message from MySQL with the text "No such function." I just did a grep on the whole source tree of MySQL 5.1, and that string does not occur anywhere (except in one comment).
My thought is that you aren't using MySQL, you're using SQLite. Because I can reproduce that error when I run the SQLite command-line shell:
$ sqlite3
sqlite> select curdate();
Error: no such function: curdate
sqlite> select now();
Error: no such function: now
In SQLite, the function to get the current date is simply date():
sqlite> select date();
2010-01-02
Many functions are different in SQlite and in MySQL (or any other product - if you except core functions, most functions provide the same functionality but with a different syntax). There is an open-source "compatibility library" implementing a large number of MySQL functions in SQLite on the Kansas State University CIS website
My inclination is to run a mysql repair install. if that didn't work, then i'd try to wamp reinst.
If SQLite had the same features as SQL, it would be amazing. But fortunately for query types with date comparisons it is possible to use existing SQLite functions.
An example of how I was able to carry out my query was as follows:
Using an interface type class declaring my ObjectDAO: 'PartidoDAO'.
#Query("select * from partido where fpartido>=DATE() and epartido = 'Santiago Bernabeu' ORDER BY fpartido LIMIT 1")
Partido getMoreRecentPartido();
If you want more information about functions related to date data types consult the web I invite you to consult this resource.
SQLite Date Functions.