HomeОбразованиеRelated VideosMore From: BeardedDev

SQL Tutorial - UPDATE

3 ratings | 170 views
This video will teach you how to UPDATE data within a table with working examples, within SQL Server, we also look at the dangers of running an UPDATE statement without a WHERE clause and the impact this will have. Remember: Always take a backup of your database when running DML scripts Test your script locally Add a WHERE clause unless you want to update all data within the table Statements: UPDATE ‘table_name’ SET ‘column_name’ = ‘value’ WHERE ‘column_name’ = ‘value’ UPDATE Customers_New SET C_Last_Name = 'Masters' WHERE C_Id = 4 UPDATE multiple columns: UPDATE Customers_New SET C_1st_Line = '10 Wayside', C_2nd_Line = 'Withington', C_City = 'Telford', C_PostCode = 'TF2 1SH' WHERE C_Id = 2 UPDATE data based on a join: UPDATE A SET A.C_Last_Name = B.C_Last_Name FROM Customers AS A INNER JOIN Customers_New AS B ON A.C_Id = B.C_Id WHERE A.C_Last_Name 'not equal too' B.C_Last_Name UPDATE statements (DML) are covered in the Microsoft Exam – Querying SQL Server (70-461) 2012 / 2014. To learn how to backup and restore databases check out the video below: SQL Tutorial – Backup and Restore https://youtu.be/rtX0T_bK5-0
Html code for embedding videos on your blog
Text Comments (8)
Moepi Maile (1 year ago)
Sir how can i get your response quickly please
BeardedDev (1 year ago)
Hi, please check out this link: https://youtu.be/bfBnZKf2EGg
BeardedDev (1 year ago)
Hi, I am just uploading a video on creating tables with constraints, that should be up in the next hour.
Moepi Maile (1 year ago)
Find the exact time Write a single SQL statement to list all the tracks that have the exact word 'time' (including both upper and lower cases) as part of the name in the Track table. In other words, it will not include track names with words like 'wartime' or 'times' in the output. How would you solve this one pls
BeardedDev (1 year ago)
SQL Server isn't case sensitive by default, you can just write: SELECT [columns] FROM [table] WHERE [column] LIKE '% time %' You can add spaces within the like so it will only show the exact word within a string, let me know if you'd like me to do a video on this and I will put one up.
Moepi Maile (1 year ago)
Thank you
Moepi Maile (1 year ago)
Hi Can you help me please "Yo are told that one employee has a phone number and fax number stored with a missing + sign at the front but you do not know which employee has done that. All other employees have their contact numbers starting with the + sign. Write a single SQL statement (without using subquery) to update that particular phone number and fax number by adding a + sign at the front.
BeardedDev (1 year ago)
Hi, One option you have is to use a NOT LIKE within the WHERE clause in your UPDATE statement. UPDATE [table] SET [column] = '+' + [column] WHERE [column] NOT LIKE '+%' Here is a sample of the data I think you are referring to: EmpId PhoneNumber FaxNumber 1 +5345345345 +323123123 2 +3423432432 +234325541 3 4267676546 654654325 You can actually do this with one UPDATE statement as you need the + in both columns, so for example if a customer is only missing the + in telephone number, you can still UPDATE both columns as this would be correct. UPDATE dbo.Employees SET PhoneNumber = '+' + PhoneNumber, FaxNumber = '+' + FaxNumber WHERE PhoneNumber NOT LIKE '+%' OR FaxNumber NOT LIKE '+%' Final Result: EmpId PhoneNumber FaxNumber 1 +5345345345 +323123123 2 +3423432432 +234325541 3 +4267676546 +9654654325 If you are dealing with a large amount of data it will not be great for performance using a LIKE but in this case you have to search the string, I am guessing this is going to be run as a one off though. If you need any more help let me know. BeardedDev

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.