r/SQL 20h ago

MySQL need help with tihs standardization query

this is a distinct list of warehouse names from a table in the db im using to practice data cleaning in mysql. i want to capitalize the initials of all words in the column. i made my own logic for this whihch is (dont judge pls im a self learner)

and this is the output i get:

i do get what im doing wrong to get this output, but i can not figure out how to go about the standardization. how can i correct my query? and is there a more efficient way of capitalizing initials than this?

1 Upvotes

7 comments sorted by

2

u/Imaginary__Bar 20h ago

Your images aren't showing up (there is a placeholder saying "this image has probably been deleted")

Paste your query text instead (as per the FAQ)

1

u/haligma 20h ago

theyre showing up for me on my pc where i made the post, but yes i cant see it on my phone either. heres the query

UPDATE dirty_shipments

SET origin_warehouse = CONCAT(UPPER(LEFT(origin_warehouse, 1)), LOWER(SUBSTRING(origin_warehouse, 2)), UPPER(RIGHT(origin_warehouse, 1)));

origin_warehouse being the column i want to modify the values in.

Warehouse A
warehouse b
Warehouse C
Warehouse D

this is the original distinct table.

Warehouse aA
Warehouse bB
Warehouse cC
Warehouse dD

and this is the output i get. i understand why its that way, but im not sure how to fix it

1

u/Imaginary__Bar 20h ago edited 19h ago

In MySQL substring(<string>,2) will just give you all the characters from the second character onwards.

I would do something clumsy like;

Concat(\ Upper(substring(origin_warehouse , 1, 1)),\ Lower(substring(origin_warehouse, 2, Length(origin_warehouse)-2),\ Upper(substring(origin_warehouse, -1))\ )

You can use Left() Mid() and Right() for the same effect, but I don't know which is more performative.

1

u/haligma 20h ago

Upper(substring(origin_warehouse , 1, 1)

wait, 3 arguments in the substring function? how does that work?

and by len(), do you mean the length() function?

Upper(substring(origin_warehouse, -1)

lol i tried this too, but it came out wrong. but ig in this could, this segment works like how its supposed to?

1

u/Imaginary__Bar 19h ago

Three arguments in the substring function are; string, start_character, length_of_substring

Substring (text, 1, 1) is the same as Left(text, 1)

Yes Len should be Length (I already edited that)

That last one should work fine (gives the last character in the string). Edited to add; needs one more closing bracket...

2

u/haligma 19h ago

YAAAAAAAAAH I UNDERSTAND THIS SO PREFECTLY & IT WORKEDDDDDDDD THAKN YOU@!!!!!!!!!!!!!

1

u/Imaginary__Bar 19h ago

Apols - what I wrote needed one more closing bracket (I've edited it now)