Deleting values in a coloumn except first 7 values - mysql

Trying to delete values in a column except the first 7
Column combine values= "0000396Memorial Hosp of Wm F & Gertrude F Jones A/K/A Jones Memorial Hosp"
I basically need just the first 7 values = 0000396
Can anyone please guide me to the right direction?

You can use the LEFT() string function to return the 7 leftmost characters:
UPDATE table SET column = LEFT(column, 7)

Assuming that I am understanding the question correctly, you can
UPDATE table SET column = SUBSTRING(column FROM 1 FOR 7);

Related

Update query to replace 003 to 030 in a 9 digit values of a field in table in access

I have an access table with a numeric field. It was 9 digit field and all values start with 003, But I want it to start with 030. Please help me to make an update query to solve this problem.
assuming you have string column of 9 char length you could repalce the leftmost 3 char using
update my_table
set my_column = '030' & right(my_column, 6)
where left(my_column,3) = '003'

How do I Query for used BETWEEN Operater for text searches in MySql database?

I have a SQL Table in that i use BETWEEN Operater.
The BETWEEN Operater selects values within range. The values can be numbers, text , dates.
stu_id name city pin
1 Raj Ranchi 123456
2 sonu Delhi 652345
3 ANU KOLKATA 879845
4 K.K's Company Delhi 345546
5 J.K's Company Delhi 123456
I have a query like this:-
SELECT * FROM student WHERE stu_id BETWEEN 2 AND 4 //including 2 & 4
SELECT * FROM `student` WHERE name between 'A' and 'K' //including A & not K
Here My Question is why not including K.
but I want K also in searches.
Don't use between -- until you really understand it. That is just general advice. BETWEEN is inclusive, so your second query is equivalent to:
WHERE name >= 'A' AND
name <= 'K'
Because of the equality, 'K' is included in the result set. However, names longer than one character and starting with 'K' are not -- "Ka" for instance.
Instead, be explicit:
WHERE name >= 'A' AND
name < 'L'
Of course, BETWEEN can be useful. However, it is useful for discrete values, such as integers. It is a bit dangerous with numbers with decimals, strings, and date/time values. That is why I encourage you to express the logic as inequalities.
In supplement to gordon's answer, one way to get what you're expecting is to turn your name into a discrete set of values:
SELECT * FROM `student` WHERE LEFT(name, 1) between 'A' and 'K'
You need to appreciate that K.K's Company is alphabetically AFTER the letter K on its own so it is not BETWEEN, in the same way that 4.1 is not BETWEEN 2 and 4
By stripping it down to just a single character from the start of the string it will work like you expect, but take cautionary note, you should always avoid running functions on values in tables, because if you had a million names, thats a million strings that mysql has to strip out to just the first letter and it might no longer be able to use an index on name, battering the performance.
Instead, you could :
SELECT * FROM `student` WHERE name >= 'A' and name < 'L'
which is more likely to permit the use of an index as you aren't manipulating the stored values before comparing them
This works because it asks for everything up to but not including L.. Which includes all of your names starting with K, even kzzzzzzzz. Numerically it is equivalent to saying number >= 2 and number < 5 which gives you all the numbers starting with 2, 3 or 4 (like the 4.1 from before) but not the 5
Remember that BETWEEN is inclusive at both ends. Always revert to a pattern of a >= b and a < c, a >= c and a < d when you want to specify ranges that capture all possible values
Compare in lexicographical order, 'K.K's Company' > 'K'
We should convert the string to integer. You can try that mysql script with CAST and SUBSTRING. I've updated your script here. It will include the last record as well.
SELECT * FROM student WHERE name CAST(SUBSTRING(username FROM 1) AS UNSIGNED)
BETWEEN 'A' AND 'K';
The script will work. Hope it will helps to you.
Here I've attached my test sample.

SQL updating data with duplicate information

So i've been working on this query for awhile and for some reason whenever i run it, it tries to update certain fields that are already equal in both tables. For example take these two tables:
AlldataTable
first__last__age__number
K____ D __ 17____ 7
K____ D __ 15____ 7
X____ X___ 12 ____21
NewDataTable
first__last__age__number
K____ D __ 17____ 7
K____ D __ 15____ 7
X____ X___ 13 ____22
so the goal here is to have the age field for x x update to 13 and the number field to update to 22 when the query is executed. But when i execute the code it would say for example that it is going to update all three entries when just the third should be.
UPDATE AllDataTable
LEFT JOIN NewDataTable ON (AllDataTable.first = NewDataTable.first)
AND (AllDataTable.last = NewDataTable.last)
SET AllDataTable.age = NewDataTable.age
,AllDataTable.number = NewDataTable.number
WHERE (
AllDataTable.age <> NewDataTable.age
OR AllDataTable.number <> NewDataTable.number
)
AND AllDataTable.first = NewDataTable.first
AND AllDataTable.last = NewDataTable.last;
It's because of your duplicated values in first and last. The database is seeing three hits on the WHERE clause:
Alldata first row against NewData second row.
Alldata second row against NewData first row.
Alldata third row against NewData third row.
This gives the appearance of only one row changing, but you get an affected row count of three because the first and second rows actually swap all their contents in addition to the third row being updated.

Update multiple rows with multiple where clauses for each row. (update_batch can only handle 1 where clause) [duplicate]

This question already has an answer here:
Update multiple rows with multiple 'where' clauses for each individual row
(1 answer)
Closed 9 years ago.
I am trying to update my table like this:
Update MyTable
SET value = 1
WHERE game_id = 1,
x =-4,
y = 8
SET value = 2
WHERE game_id = 1,
x =-3,
y = 7
SET value = 3
WHERE game_id = 2,
x = 5,
y = 2
I can do a foreach() but that will send over 50 separate Queries which is very slow.
That's why I want it to be combined into 1 big Query.
( I do use an id for each row but the combination of game_id, x and y is what I use to Identify the row I need. )
The update_batch() function from codeIgniter described here:
Update batch with CodeIgniter
was helpful and almost perfect but it only allows for 1 single where clause, you cannot (as far as I understood and tried) enter an array with multiple where clauses.
I've also checked out this question:
MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses
But it only allows for multiple row updates containing only a single different WHERE clause and I need multiple WHERE clauses! :)
Anwsers can be in simple SQL or with the use of php (and CodeIgniter) or in a different way. I'd this problem to be solved in any possible way ;)
I can really use the advice/help! =D
EDIT:
Sorry the question wasn't clear enough, I just changed it!
It seems me you need the logical operator AND:
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8
I'm not familiar with the multiple WHERE clauses how you have it laid out, why not just do
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8

MySQL add data to an existing field

I have a field enq_id - it currently contains numbers such as:
80081
414365
567
Now, I need to update the field in two ways, firstly I need to find out if any of the numbers are more than 6 characters long (there shouldn't be but I need to check). I then need to set a character limit of 6 characters on that field and then, finally, add 0's to the beginning of any enq_id that isn't 6 characters long to make it 6 characters.
Any ideas?
If the following returns the result you're trying to achieve:
SELECT
`enq_id`,
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6)
) AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) != 6
Then using the following will update your table accordingly.
UPDATE `some_table`
SET `enq_id` =
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6))
WHERE CHAR_LENGTH(`enq_id`) != 6
Note that the SUBSTRING() function deletes all the characters after the 6th character, and LPAD adds preceding zeros (in the above example) if needed, to each record.
UPDATE: For some reason I added an extra condition. The optimized code(s) should have been:
SELECT `enq_id`,LPAD(`enq_id`,6,'0') AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) < 6
and
UPDATE `some_table`
SET `enq_id` = LPAD(`enq_id`,6,'0')
WHERE CHAR_LENGTH(`enq_id`) < 6