Obtain result set from table within column range? - mysql

How to obtain ResultSet from DB table within range ?
String sql=SELECT * From Employee Where Salary<=? AND Salary>=?;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
String lowest_salary=In.nextLine();
String max_salary=In.nextLine();
preparedStatement.setString(1,lowest_salary);
preparedStatement.setString(1,max_salary);
ResultSet resultSet = preparedStatement.executeQuery();
Can I assigned values as parameter twice to DB table column(EmployeeSalary)? What's solution?

You are close. Your main issue is that you are using .setString(1, ...) twice, which is incorrect. You have two parameter placeholders (?) in your SQL command text so you need to set the first parameter value with the index 1 and the second parameter value with the index 2.
Also be sure that you set the parameter values using the method that matches the column type. .setString is to be used with text columns. A column named "Salary" is more likely to be numeric, so you should probably be using .setInt if the column is integer, or .setBigDecimal if the column is a decimal or currency type.

Related

replace all column values equal to one string with another string

I have a MYSQL table called traceclasses and I have a column in this table called value with some of its cell values equal to E as shown below. I would like to replace all such values with U. Which query should I use to do this?
You are looking for update:
update t
set value = 'U'
where value = 'E';

how to properly do the following update MYSQL statement?

i have a string with the following value
Germany,Sweeden,UAE
and i have an mysql row that have a value of one of this separated string with comma
as example a Country Row with a value Sweeden .
i have tried to use this sql statement to update a targeted row where its value is like one of this string separated with comma
hence if the row have a value of sweeden it should be updated sense sweeden value is include in the comma string
UPDATE CountryList SET Time= '100' WHERE Country LIKE '%Germany,Sweeden,UAE%'
but i couldn't update the targeted row using this syntax. any idea what is the correct syntax i should use ?
I think you want find_in_set():
update CountryList
set time = 100
where find_in_set(country, 'Germany,Sweeden,UAE')
This checks if country matches any of the values in the comma-separated row given as second argument to find_in_set().
Note that I removed the single quotes around the literal 100: if time is a number, then treat it as such (if it's a string instead, then keep the quotes).

How to convert a 187.5 string to number and insert in Decimal(3,1) column in mysql?

Problem : I need to convert a string value "187,5" and insert into a data type DECIMAL(3,1)
I have a mysql table called "betygpoang" with a "poang" field and the datatype is set to DECIMAL(3,1) and a "betyg" table with a "meritvarde" field and the datatype is set to DECIMAL(3,1) here as well, and Im using asp classic.
I sum and get a decimal value from "poang" that I need to update "meritvarde" with in the "betyg" table, the problem is that the sum value I get back is a text string "187,5" with a comma(,) even if it is a point(.) in the database.
So I have to convert "187,5" to "187.5" a proper decimal value to be able to insert it to my db.
I select and sum it, and update it like this.
sql = "SELECT sum(poang) FROM betygpoang"
set rs = conn.Execute (sql)
meritvarde=CDbl(rs19("sum(poang)"))
meritvarde=replace(meritvarde,",",".")
sql= "UPDATE betyg SET meritvarde = "& meritvarde;"
conn.Execute (sql)
So here is "meritvarde" "187.5" but still a string. And with this I get an error saying "Out of range value for column 'meritvarde'" so its not the right data type since its a string I guess.
But how can I convert it to be the right format?
If I use CINT then it rounds it up to 188.
DECIMAL(3,1) -> Max is 99.9
if you want to store 187.5 , change to DECIMAL(4,1) -> Max is 999.9

How to select columns which are not null in SAP HANA?

So I have a base table - TRAINING, which has 100 columns. Some of the columns will be completely NULL and some will contain Values. So say COLUMN 1-20 are null and COLUMN 21-100 are not NULL.
I have another table called - CONFIGURATION. It has only one column of type VARCHAR. This table contains the name of those columns from the TRAINING table that are not NULL. So it'll contain values - COLUMN 21-100.
What I want to do is- fetch the data of only those columns that are not NULL. So I want the output as the data points contained in table COLUMN 21-100. This number may be different every time and it can also be interleaved, say COLUMN 1-10 is NULL and COLUMN 11-25 not NULL and the remaining again NULL.
I am thinking of implementing inner Join but I do not have the table structure required for it.
Please provide some hint.
Thanks.
You need to create dynamic SQL for that.
First step - create ALL_COLUMNS variable of VARCHAR(5000) data type.
From your CONFIGURATION table select column names which you want to query. Then use STRING_AGG function to aggregate them into single value (in my example COL1 is column from CONFIGURATION table). Assign output to the ALL_COLUMNS variable
Second step use EXECUTE IMMEDIATE to run dynamic SQL. Add ALL_COLUMNS variable as input for that query.
Here is the examplary code:
DO
BEGIN
/* First Step - create string with all column names separated by comma*/
DECLARE ALL_COLUMNS VARCHAR(5000);
SELECT STRING_AGG(COL1,',' ORDER BY COL1) INTO ALL_COLUMNS FROM CONFIGURATION;
/*Second Step - create dynamic SQL including variable from First Step*/
EXECUTE IMMEDIATE ('SELECT ' || :ALL_COLUMNS || ' FROM "TRAINING" ');
END

mysql syntax for populating a column of an existing table and setting a default value for said column

I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?