Тёмный

SQL Interview Query | How to pad zeroes to a number | LEFT 

Learn at Knowstar
Подписаться 44 тыс.
Просмотров 11 тыс.
50% 1

In this video, we write a query to pad zeroes to a number.
Check out the complete list of SQL Query Interview Questions -
• SQL Query Interview Qu...
Please do not forget to like, subscribe and share.
For enrolling and enquiries, please contact us at
Website - knowstar.org/
Facebook - / knowstartrainings
Linkedin - www.linkedin.com/company/know...
Email - learn@knowstar.org

Опубликовано:

 

28 мар 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 21   
@cockbeard
@cockbeard 2 года назад
Could I recommend to all viewers that you do not use '+' to concatenate items. It is a mathematical operator, and if you are unaware of the data types you are working with it can lead to errors and a lot of debugging. It was standard practise ten years ago, then SQL2012 gave us the CONCAT() function which also converts any input to a string. In the example given in the video the @var was created as a 'varchar' however in your own data it would be far more likely stored as an 'int', wherein this query will return an error. You will in your professional life often see legacy code using the method detailed above, however try not to get into bad habits so early in your learning, and don't be afraid to correct existing codebases and implement coding standards. So if you're working on SQL Server since 2012, which really you should be, then use CONCAT(). The FORMAT() option described elsewhere has the same (but inverse) problem, being as FORMAT() will not work with string datatypes Using CONCAT() is datatype agnostic and therefore a far better habit to be in. See example below DECLARE @var1 VARCHAR(5) = '23' DECLARE @var2 INT = 23 SELECT RIGHT(CONCAT('0000',@var1),4) SELECT RIGHT(CONCAT('0000',@var2),4)
@doitek
@doitek Год назад
Ur Voice... Superb !!!
@subhadarshisethy1680
@subhadarshisethy1680 2 года назад
Ty mam...
@oclik1876
@oclik1876 2 года назад
Or we can just do that DECLARE @var varchar(50), /* whatever length */ @needLength int; /* the final length of sequence */ SELECT CONCAT(REPLICATE('0', @needLength - LEN(@var)), @var) this will compute the needed number of '0' pads and concat it with value NOTE: if LEN(@var) is greater than @needLength REPLICATE will return NULL but CONCAT will ignore that and just return the initial value
@KamaleshUbbu
@KamaleshUbbu 2 года назад
Right or left function can create problems with data loss if the input number is beyond 4 digit. Best solution would be to use format function
@aadarshchaudhari3957
@aadarshchaudhari3957 9 месяцев назад
Thanks
@LearnatKnowstar
@LearnatKnowstar 8 месяцев назад
Thank you
@sravankumar1767
@sravankumar1767 2 года назад
Superb explanation 👌 👏
@LearnatKnowstar
@LearnatKnowstar 2 года назад
Thank you so much
@tathagatsharma9225
@tathagatsharma9225 2 года назад
A more direct solution can be use of FORMAT clause. Select FORMAT(4,'0000')
@LearnatKnowstar
@LearnatKnowstar 2 года назад
That's right
@cockbeard
@cockbeard 2 года назад
Unfortunately this method will definitely not work with the code as shown in the video. FORMAT() will not work with string datatypes, and in this video @var is defined as VARCHAR, a string
@rpij688
@rpij688 2 года назад
Since the title states that the datatype is a number, in my opinion FORMAT is the preferred method. It also works for negative numbers.
@cockbeard
@cockbeard 2 года назад
Number != Numeric, oh actually != isn't ANSI compliant, let me fix that Number Numeric
@srinuvenkata8567
@srinuvenkata8567 Год назад
If the value is numeric (INT/BIGINT) FORMAT function works perfectly otherwise RIGHT function works perfectly if the datatype is varchar(50)
@pradeepba1439
@pradeepba1439 2 года назад
Regexp videos pls
@LearnatKnowstar
@LearnatKnowstar 2 года назад
Coming soon. We will be posting soon in coming weeks
@pradeepba1439
@pradeepba1439 2 года назад
@@LearnatKnowstar thanks...
@sourabhpatel3834
@sourabhpatel3834 3 месяца назад
format('col' , '0000')
@rravirao1273
@rravirao1273 2 года назад
Dear Ma'am, Could you please help us with float data type for this scenario? Input : 15.33 159.14 1142:47 output : 0015.33 0159.14 1142:47
@cockbeard
@cockbeard 2 года назад
Read my comment and examples, it will work for numeric or string data
Далее
Редакция. News: 125-я неделя
48:25
Просмотров 1,5 млн
SQL Debugging | Common Programming Mistakes | Dates
8:03
Learn GROUP BY vs PARTITION BY in SQL
14:03
Просмотров 33 тыс.