Consider the following two strings,
source
-------
'ADAM' -- 4 chars length
'BOB' -- 3 chars length
I want to concatenate spaces after the strings where the number of spaces + length of string(n) should not exceed a specific number.
So the output should look something like the below where n = 8 in this case.
result
-----------
'ADAM ' -- 8 chars length
'BOB ' -- 8 chars length
How can I do this in mysql dynamically?
I could check the length of the field and use case statements for each scenario but thats not ideal. I am using mysql 8.0.17.
You would use the function rpad():
select rpad(name, 8, ' ')
Related
Allowed characters are 0-9 A-Z a-z and .-_
Ich want to search in MySQL Database in one column if there some other characters as allowed and replace them with "-".
Is it possible with SQL?
Something like this ;)
#ARRAY = (0,1,2,3,4,5,6,7,8,9,.,-,_,A-Z,a-z);
UPDATE table SET columnName = replace(columnName, 'CHAR DOES NOT MATCH #ARRAY', '-');
UPDATE:
ID NUMBER
1 1620WGC-2018_3.2
2 70-30-2015
3 PTMMC4450-2017+
4 PE-1013-64/2018
5 1580-2018_3%2
6 PE-1036-68A-2018
7 D10+
In Column NUMBER the ID's 3,4,5 and 7 shoud be corrected to:
ID NUMBER
1 1620WGC-2018_3.2
2 70-30-2015
3 PTMMC4450-2017-
4 PE-1013-64-2018
5 1580-2018_3-2
6 PE-1036-68A-2018
7 D10-
because +,/, and %, for example, are not in Array with allowed charakters.
You might need this.
SELECT * FROM table WHERE replace(replace(columnName, '\'', ''), '-', '') = 'findMyText'
I need to display output data, I need to add/replace with white spaces with fix length for each column. Each column may got different length.
select top (20)
patname + REPLICATE(' ', 30 - DATALENGTH(patname)) AS NAME,
RefNo + REPLICATE(' ', 15 - DATALENGTH(RefNo)) AS REFNO,
ClaimAmt AS AMOUNT
from AR_Ebilling
Field length need to be set:
NAME = 30 varchar, REFNO = 15 varchar, AMOUNT = 10 money/decimal,
Expected Output Result :
NAME REFNO AMOUNT
Ahmad Kasan 1235 00000000565.93
Amirah AY582M8D -00023441200.23
Paul 0ST127 00000004234.45
TQ
The answer is that don't! One does not store redundant data in databases. All those white spaces are not really required to be stored because it's a trivial matter to pad out a string in your favourite programming language or even with in mysql itself. Towards this end mysql has :
RPAD
Returns the string str, right-padded with the string padstr to a
length of len characters. If str is longer than len, the return value
is shortened to len characters.
And
LPAD
Returns the string str, left-padded with the string padstr to a length
of len characters. If str is longer than len, the return value is
shortened to len characters.
Also note that needlessly storing padded strings in the table makes it futile to use VARCHAR.
I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.
A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);
If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;
Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx
I'm working with 2 sets of data that were merged together, but they're inconsistent in their format. Some are 10 characters, all numbers. Others may have a separator : at position 4. I need to substring the first 4 characters. But if the 4th character is a : substring only the first 3 characters.
Does mysql have an IF functionality to determine the number of characters to substring based on the character in position 4?
select substring(id, 1 , 3/4) from table1
You can treat the field like it's colon separated and do this to select only the first part:
SELECT SUBSTRING_INDEX(id, ':', 1)
See also: SUBSTRING_INDEX()
I have a varchar column that I am currently sorting by using: ORDER BY (col_name+0)
This column contains both digits and non-digits, and the result of this sorting is this:
D3
D111
M123-M124
M136
4
9
10
25
37b
132
147-149
168b
168ca
This sorting is almost perfect for our application, but with one exception: we want the items that start with letters to display after those that start with numbers. This being the ideal result:
4
9
10
25
37b
132
147-149
168b
168ca
D3
D111
M123-M124
M136
I'm hoping this can be achieved in the select statement, rather than needing to loop through everything in code again after the select. Any ideas?
You can use this:
ORDER BY
col_name regexp "^[^0-9]",
case when col_name regexp "^[0-9]" then col_name + 0
else mid(col_name, 2, length(col_name )-1) + 0 end,
col_name
this will put rows that begins with a digit at the top. If col_name begins with a digit, I'm sorting by it's numeric value, if not I'm sorting by the numeric value of the string beginning at the second character.