sql - Add 0 after a numeric value - mysql

I have columns in my sql table. I am wondering how can I add zero after numeric values in my columns. So for example:
I have values e.g 9, 2, 7, 10. I want to add a zero after these numbers. I want them to be 90, 20, 70, 100.
There are some values in the columns that already have 0s after them e.g 70, 20, 100. These ones should retain their values.
How do I go about this?

Just multiple the column by ten (not sure what your table name is, edit that query):
UPDATE __TABLE__
SET mt_ca1 = (mt_ca1 * 10),
mt_ca2 = (mt_ca2 * 10)
WHERE mt_ca1 < 100 OR mt_ca2 < 100;
It is not clear if you want to add zeroes after only double-digit numbers or all numbers. MySQL has substring functionality though you'd need to clarify your needs.

You can use CONCAT and CASE
assuming the column type is INT
use CASE to check if the value multiplied to 10 is less than or equal to 100.
SELECT
(CASE
WHEN (mt_ca1*10) <= 100
THEN CONVERT(CONCAT(mt_ca1,'0'),UNSIGNED INTEGER)
ELSE mt_ca1
END) AS mt_ca1
FROM yourtable

Related

Get unique values from MySql

In my table are values like: 14, 15, 14, R14, R15, R15C, R14C,....
With current distinct mode I get: 14, R14, R14C, R15, R15C,...
But what I want is only unique numbers like this: 14, 15,...
To get rid of letters and get unique numbers only?
My current code:
SELECT DISTINCT diameter FROM tires WHERE category = '$cat' ORDER BY diameter ASC
SELECT DISTINCT 0 + REGEXP_REPLACE(diameter, '^[[:alpha:]]*', '') diameter
FROM tires;
Regular expression removes all letter chars from the beginning, then the value is implicitly converted to numeric by adding zero (by fact - trailing letters are removed).
fiddle
I forget to tell that I have decimal values like R22,5 - RoX
Replace comma with dot before or after REGEXP_REPLACE(), but before adding zero. For example,
SELECT DISTINCT 0 + REGEXP_REPLACE(REPLACE(diameter, ',', '.'), '^[[:alpha:]]*', '') diameter
FROM tires;
fiddle
PS. I'd recommend to add generated column to the table structure. See example.

Get value between from to dataset columns ssrs

I have a data set like that:
Data Set Contents
From To Comment
----+---+--------
0 50 Bad
50 70 Good
70 100 Excellent
If I have a value of 75, I need to get Excellent by searching the Dataset.
I know about the lookup function but it is not what I want. How can I do that?
The values should be in percentage.
Note : the value (75) is Average of a column (Calculated) it
calculate student grade from max and student mark Version SQL Server
2016
Note 2 : the dataset is from database not static values
Thank You
Assuming you only ever have a fixed number of 'grades' then this will work. However, I would strongly recommend doing this type of work on the server where possible.
Here we go...
I created two datasets
dsGradeRange with the following sql to recreate your example (more or less)
DECLARE #t TABLE (low int, high int, comment varchar(20))
INSERT INTO #t VALUES
(0,49,'Bad'),
(50,69,'Good'),
(70,100, 'Excellent')
SELECT * FROM #t
dsRandomNumbers This just creates 30 random numbers between 0 and 100
SELECT *
FROM (SELECT top 30 ABS(CHECKSUM(NEWID()) % 100) as myNumber FROM sys.objects) x
ORDER BY myNumber
I added a table to the report to show the grades (just for reference).
I then added a table to show the dsRandomNumbers
Finally I set the expression of the 2nd column to the following expression.
=SWITCH
(
Fields!myNumber.Value < LOOKUP("Bad", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Bad",
Fields!myNumber.Value < LOOKUP("Good", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Good",
True, "Excellent"
)
This gives the following results
As you can see we only need to compare to the high value of each case, the first match will return the correct comment.
Right click on your dataset and add a calculated field. Go to Field Properties > Fields > Add and add the following expression, which descripes your scenario:
=IIF(Fields!Number.Value < 50, "Bad", "Good")

MYSQL replace part of string with condition of char is numeric

Need help with this, as I am stumped with how to set the query update.
I will like to replace occurrences of 10x to 20x where x must be a numeric number. Also the numeric number can occur in any position. And the first two numeric digit of the numeric occurrence must be a 10
eg: tableA - at field colA has the followings:
TOK101s
102YUZ
TAIP103v
ECC10
ECC10a
SCC_103
TD-102b
ZA1104z
Result after the query update should be:
TOK201s
202YUZ
TAIP203v
ECC10
ECC10a
SCC_203
TD-202b
ZA1104z
ECC10 and ECC10a should not be updated since the 3rd char after 10 is not a numeric value.
ZA1104z should not be updated bec the numeric string 1104 does not begin with 10.
Thanks to Barmar and Misaka for:
SQL: search/replace but only the first time a value appears in record
update tableA set colA = IF(INSTR(colA, '10') <> 0, CONCAT(LEFT(colA, INSTR(colA, '10') - 1), '20', SUBSTRING(colA FROM INSTR(colA, '10') + CHAR_LENGTH('10'))), colA) where colA REGEXP '(^|[^0-9])10[0-9]'
The above helped me to get what I wanted.

Transforming a column to have 10 Digits

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

TSQL Filter in sproc failing on numeric conversion

I have a sub-query that returns a value. If that value is greater than a range selected by the outside query, it should return the highest value of the outside query.
Given the following:
DECLARE #ExternalPixelValue VARCHAR(MAX)
DECLARE #SecondsTriggerValue INT
DECLARE #Trigger INT
SELECT TriggerValue
FROM vw_ContainerBehaviouralPixels
WHERE TriggerValue < 9000
SELECT
#ExternalPixelValue = ExternalPixelValue,
#SecondsTriggerValue = CONVERT(INT, TriggerValue),
#Trigger = SUM(p.LastEventTimeSpan)
FROM
PageVisitEvents p
RIGHT JOIN
vw_ContainerBehaviouralPixels vwc ON p.AccountContainerID = vwc.AccountContainerID
WHERE
vwc.SystemBehaviouralSegmentID = 2 -- minimum time on site
AND
CONVERT(INT, vwc.TriggerValue) < (SELECT 99999)
GROUP BY
ExternalPixelValue, TriggerValue
SELECT #ExternalPixelValue, #SecondsTriggerValue, #Trigger
The first query returns values: 10, 30, 60, 90, 180, 300, 360.
Now, putting in a value in the subquery of 11 returns 10; anything between 31 and 60 returns 30, and so forth. Problem is that when I enter anything in 180, 90 is always returned. When I change the table value from 90 to 99, still, only 99 is returned.
TriggerValue is VARCHAR(512) which is why the conversion up there. If figured the filter is recognizing the character, and not the numeric value. However after using CONVERT and CAST, I'm still seeing the same issue.
Any advice?
Thanks.
You are populating the variables
#ExternalPixelValue, #SecondsTriggerValue, #Trigger
over and over, so in the end, you have no control of the values they contain (the value from the last read)
If you write a syntax like that, make sure you only get 1 result for each variable or that you don't care which result is chosen.