Related
This question already has answers here:
Compare dates in MySQL
(5 answers)
Closed last year.
when I output these two SQL operations, the columns for the two months (01 = January) and (02 = February) are summed together instead of individually. So they show the same output. But why?
If if want to output (02 February) it should not show the sum of January but it does. Where is my problem in my code?
"SELECT SUM(" + dataBaseHelper.GET_SUM + ") FROM " + dataBaseHelper.TABLE_NAME + " WHERE " + dataBaseHelper.DATE + " BETWEEN '01/01/2022' AND " + "'07/01/2022'"
and
"SELECT SUM(" + dataBaseHelper.GET_SUM + ") FROM " + dataBaseHelper.TABLE_NAME + " WHERE " + dataBaseHelper.DATE + " BETWEEN '01/02/2022' AND " + "'07/02/2022'"
The standard syntax for date in MySQL is yyyy-mm-dd. I think you could replace the dates in your where between statement using this consideration. Otherwise read string to date cast specifications.
I am trying to use a CASE statement in MySQL based on a variable. I tried the code below; however, I have found that "The SELECT statement cannot refer to system variables or user-defined variables". What I am trying to achieve is that I have an input date. I get its day of week so I know which day it is on (1 is Sunday). I then only want rows returned in the query that have the day set to "Y" (e.g., if day of week is 1 then stt_sunday is "Y"). The table has:
stt_id
stt_start_date
stt_end_date
stt_start_time
stt_end_time
stt_sunday
stt_monday
stt_tuesday
stt_wednesday
stt_thursday
stt_friday
stt_saturday
My code is:
String selectQry1 = (" SET #clickedDateDOW = clickedDateDOW; " +
" SELECT st_type, csm_session_timetable.stt_id, stt_start_date, stt_end_date, stt_start_time, stt_end_time " +
" FROM csm_session_type, csm_session_timetable " +
" WHERE csm_session_type.cli_id = ? " +
" AND csm_session_type.prm_id = ? " +
" AND csm_session_type.st_id = csm_session_timetable.st_id " +
" AND (csm_session_timetable.stt_end_date IS NULL || csm_session_timetable.stt_end_date > ?) " +
" AND csm_session_timetable.gym_id = ? " +
" AND CASE " +
" WHEN #clickedDateDOW = 1 AND stt_sunday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 2 AND stt_monday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 3 AND stt_tuesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 4 AND stt_wednesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 5 AND stt_thursday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 6 AND stt_friday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 7 AND stt_saturday LIKE 'Y' THEN 1 " +
" ORDER BY stt_start_date DESC, stt_start_time, stt_end_date DESC;");
I am trying to use a prepared statement to copy one row to a new row (INSERT)in the same table and include some static values. However, I get the following error:
SQLException in copyProgram: java.sql.SQLException: Operand should contain 1 column(s)
My input is:
prId: 4 accountID: 50 newFromDate: 2020-04-27 newToDate: 2020-04-28
My code is:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " (SELECT (?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " FROM at_program "
+ " WHERE pr_id = ?);");
ps = c.prepareStatement(insertQry, Statement.RETURN_GENERATED_KEYS);
// Create a statement and execute the query on it
ps.setString(1, accountID);
ps.setString(2, fromDate);
ps.setString(3, toDate);
ps.setString(4, prID);
ps.executeUpdate();
You have parens around the field list in your SELECT! Remove them.
The issue is that SELECT expects a list of (individual) columns, but you are passing a multi-column value.
SELECT x, y, z sees x, y and z as three values each consisting of one column, yet you are passing SELECT (x, y, z) which is interpreted as one value with three columns, hence the error.
By the way, the parens around the whole SELECT are superfluous as well, this time because INSERT INTO table (columns) SELECT ... is a recognized syntax construct in itself.
Corrected code:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " SELECT ?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes "
+ " FROM at_program "
+ " WHERE pr_id = ?;");
Get an error when writing to the database
The function for it:
var newMsg = { payload: msg.payload };
newMsg.topic="insert into MyTable (a,b,c,d,e,f,g) values (newMsg.payload)"
The incoming payload debug shows
payload: "B0:AC:A2:AC:07:F4","Ready","893901","860990","online","876","333"
The error I get from the database node (nore-red-node-mysql) is
"Error: ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value
count at row 1"
The strange thing to me is that if I try a
newMsg.topic="insert into MyTable (a,b,c,d,e,f,g) values (\"B0:AC:A2:AC:07:F4\",\"Ready\",\"893901\",\"860990\",\"online\",\"876\",\"333\")"
it works perfectly...
Where is the trick?
There is no trick.
This is because the node-red-node-mysql and node-red-contrib-sqldbs nodes do not do any query substitution.
This means that what gets sent to the database is exactly what is in the msg.topic field. In this case that would have been:
insert into MyTable (a,b,c,d,e,f,g) values (newMsg.payload)
Which mysql will read as trying to pass a single value to a query expecting 7 values.
You will have to build the full query (and do your own variable escaping if needed) in a function node before passing the message to the database node.
at the end I solved it this way:
var data = msg.payload.split(",");
msg.payload = {};
msg.payload.a=data[0];
msg.payload.b=data[1];
msg.payload.c=data[2];
msg.payload.d=data[3];
msg.payload.e=data[4];
msg.payload.f=data[5];
msg.payload.g=data[6];
insert into MyTable (a,b,c,d,e,f,g) values ('" + data[0] + "','" + data[1] + "','" + data[2] + "','" + data[3] + "','" + data[4] + "','" + data[5] + "','" + data[6] + "')";
return msg;
I want to convert a string like "2152012 101946" using a derived column in SSIS.
The output should be like "21/05/2012 10:19:46" to fit into a [DateTime] SQL Server 2008 field
Thanks!
You should think about enriching your date time data . You need to have a proper format like
YYYYMMDD HH:MM::SS
or something similar to it . You just can't have
YYYYMDD HH:MM:SS
If you have your data in the correct format DDMMYYY HH:MM:SS then you can use the below expression in derived column
LEN(column) == 0 ? NULL(DT_DBTIMESTAMP) :
(DT_DBTIMESTAMP)(substring(column,1,2) + "-" + substring(column,3,2) + "-" +
substring(column,5,4) + " " + substring(column,10,2) + ":" substring(column,12,2)+ ":"
+ substring(column,14,2))
DT_DBTIMESTAMP data types must be in the following format for proper conversion:
YYYY-MM-DD HH:MM:SS
Using the date & time "2012-07-30 02:03:10" as an example, your derived column would appear as:
(DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "-" + SUBSTRING([column],5,2) + "-" + SUBSTRING([column],7,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2))
The resulting output column would appear as:
2012-07-30 02:03:10.000
Recall that any missing values within a given date can be concatenated with the date values within your column. For example, using the string value "2152012 101946", your derived column expression would be written as:
(DT_DBTIMESTAMP)(SUBSTRING([column],4,4) + "- 0" + SUBSTRING([column],3,1) + "-" + SUBSTRING([column],1,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2) + ".000")
Taking this one step further, since dates in string format often are not clean, you might consider writing a conditional statement which checks the length of a value prior to concatenating & converting the values. For example, if 2012/09/30 11:59pm and 2012/10/01 11:59pm are represented as strings in a column where leading zeros are not present, the strings may appear as:
"2012930 115959"
"20121001 115959"
To account for leading zeros in the month, an if-then-else expression could be incorporated such that:
LEN(column) = 14 ? (DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "- 0" + SUBSTRING([column],5,1) + "-" + SUBSTRING([column],6,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2)) : (DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "-" + SUBSTRING([column],5,2) + "-" + SUBSTRING([column],7,2) + " " + SUBSTRING([column],10,2) + ":" + SUBSTRING([column],12,2) + ":" + SUBSTRING([column],14,2))
Note that the above expression does not take values that have a length less than 14 characters or greater than 15 characters into account. In this case, you could expand the above if-then-else expression to nest additional expressions for varying lengths.