How do I resolve this exception? - mysql

Good morning SO,
I'm having a bit of trouble with this query. It works fine when I tested it in the Workbench, but VB is throwing an exception that the column "ccID" is unknown. Am I doing something wrong? My intent is to pull the ccID field and the difference between today and the date in field ccAuthorizedUseEnd.
Your assistance would be greatly appreciated, thanks.
HERE'S THE QUERY I TESTED IN THE MySQL WORKBENCH:
SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) AS Days
FROM accounting.cc_master
HERE'S THE SAME QUERY I'M USING IN VB.NET
Dim dbQuery2 As String = "SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) as Days "

You've forgotten your FROM clause in the VB.NET version:
Dim dbQuery2 As String = "SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) as Days FROM accounting.cc_master"

Related

Excel VBA mysql query with string variable

So i'm doing this simple sql select from mysql database in my VBA code:
cmd.CommandText = "SELECT sum(operation_employee_execution_time) FROM employee_operation where employee_last_name like '" & sEmployee & "'"
and it is not working. If I do msgbox(cmd.CommandText) I see properly formatted SQL query:
Unfortunately result of that query is nothing (meaning like clause could not find a match), but if I hardcode the value of the variable like this:
cmd.CommandText = "SELECT sum(operation_employee_execution_time) FROM employee_operation where employee_last_name like 'Levkovic'"
It works perfectly...
Anyone could give me some advice here? I thought that this will be some sort of encoding problem but adding "CharacterSet=utf8;" to my connection string did not help (that column in db is utf8mb4_bin)
Thank you all for input, problem was that the Excel cell that was read to the variable sEmployee had some unicode characters.

MySqlConversionException issue

i want to insert date now into mysql database
i need to convert the format but it didn't worked
this is my code
Dim now As Datetime = Datetime.Now.ToString("yyyy-MM-dd")
but it didn't worked either
as i google this i found out that the problem is because the parameter in my connection string AllowDateTimeZero=True
but when i tried to add it in my conn string, it stil didn't worked. could someone help me about this
i've spent 2 hours trying to fix this, but it's not worked..
thanks before
In VB, a DateTime data type holds the number if TICKs that have occured since 1/1/0001 12:00:00 AM. When you convert the DateTime.Now into a string, then put it into another DateTime, VB must convert the string "1985-12-31" into the number of ticks.
The code you posted...
Dim now As Datetime = Datetime.Now.ToString("yyyy-MM-dd")
...does nothing except introduce the possibility of issues due to culture difference in DateTime.
What you should have done is posted the code you use to insert the data into MySQL. If you are using a command and a non-parameterized INSERT:
INSERT INTO MyTable (MyDate) VALUES (" & Datetime.Now.ToString("yyyy-MM-dd") & ")
If it is parameterized, as it should be:
cmd.AddParameterWithValue("p1",Datetime.Now)
If you are using the EF or some type of DBML:
MyTable.MyDate = Datetime.Now

Differences between SQL Fiddle and local

Over here: MySQL: Get start & end timestamp for each day I was given a working answer, but it only works for me in SQL Fiddle (http://sqlfiddle.com/#!2/62366/1). When I run the same SQL on my Debian Linux / MySQL 5.5.34 I get incorrect results (start & end times are the closest reading to midnight)! Anyone got any suggestions as to what might be causing the problem?
My mistake - real schema had another column for deviceID, and the data held readings for multiple devices. Once I added the following, all worked OK:
...
FROM myTable
HAVING deviceID = 1234
GROUP BY deviceID, StatusTime

Select Count Distinct Syntax, vb.net

So I'm starting to learn some vb.net, and I have the following mysql statement to get a distinct count for a column in one of my tables. It's been giving me fits with syntax errors, but pretty much everything I've read suggests what I have should be correct. Also, I took it into MySQL Workbench and it works fine, for whatever reason it just refuses to work in my code.
Dim sRetrieve As String
Dim numvals As OdbcDataReader
sRetrieve = "SELECT COUNT (DISTINCT defect_code) FROM daily_data WHERE MONTH(date)=" & select_month & " AND YEAR(date)=" & select_year
Dim query_exe As New Odbc.OdbcCommand(sRetrieve, cn)
numvals = query_exe.ExecuteReader()
I've been banging my head against this for awhile now, so any help telling me what's wrong with that mysql statement would be greatly appreciated.
Figured it out, the space between COUNT and the first ( was doing it. Such a hilariously small thing for it to have held me up for so long, but I guess that's how it goes. Thanks for the help, everybody.
MONTH(date) in mysql returns string. So you need to enclose with ' '.
sRetrieve = "SELECT COUNT (DISTINCT defect_code) FROM daily_data WHERE MONTH(date)='" & select_month & "' AND YEAR(date)=" & select_year;

Comparing string with SmallDateTime used to work

I'm trying to debug an old VB6 program at my job. It's full of bugs and hardcoded stuff.
The program is connecting on a SQL Server 2005 database to read and write info using stored procedures.
Since the program is still being used while I'm working on it, I made a backup of the database to test some things and restored it in my version of SQL Server 2008. Everything was working fine.
When I came in the next morning, I got an error I didn't have the previous night even though the program wasn't used in the night.
The problem :
"exec dbo.sp_Get_Wait_Data '" & DateEx & "'"
DateEx is a string containing "2012/06/14"
The stored procedure :
[sp_Get_Wait_Data] #Datest as char(10)
AS
SELECT
(A lot of column names here)
FROM
Fiche
LEFT JOIN
voyage ON fcid = vofiche
LEFT JOIN
client on fcaccount = cusnov
WHERE
fcdate = #Datest
AND (void is null or (void > 0 and (void <> 999 and void <> 1000 and void <> 998)))
AND ((fcremarques NOT LIKE '%SYSANNULATION%' OR
fcremarques IS NULL)
AND fcrettime IS NOT NULL)
ORDER BY
FcTime, FcOrgSite, fcdessite
The error message :
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value
So the error is here fcdate=#Datest in the stored procedure. I tried adding this line in the stored procedure
SELECT convert(datetime, #Datest, 120)
which worked like a charm for the convertion within the query but caused a few hundred other errors within the program when other queries tried to access this variable so it is not an option.
Any idea why this query was working perfectly yesterday and now it's giving me this error? Thanks in advance.
You probably have set dateformat dmy when connecting to the copy of your database. The date format is set implicitly by the language used. set language.
You could change the format of the parameter to YYYYMMDD which will be safe regardless of set dateformat or even better, change your parameter to datetime.
If that is not an option you can rewrite your query using where fcdate=convert(datetime, #Datest, 111)