Retrieve Numeric Value out of String SQL - sql

I have a column Property in a table Order. We used to store a dictionary(varchar) in this column.
We now have a new column (int) called ShoeSize which has a default value of 0.
So what I want to achieve is to retrieve only the numeric value out of the Property column and update the ShoeSize column with that value.
The Property column value looks like this:
ShoeSize<|$à&£#>15<|#ç§~#>
or
ShoeSize<|$à&£#>3<|#ç§~#>
My question is:
How can I manipulate my SQL in select statement to select only the numeric value of the Property column? So in other words, How would I be able to only end up with 15 or 3
Thanks in advance.

If you are using MSSQL and your format is fixed then you can try like below
DECLARE #str VARCHAR(50)
SET #str = 'ShoeSize<|$à&£#>15<|#ç§~#>'
SELECT SUBSTRING(#str,CHARINDEX('£#>',#str) + 3, CHARINDEX('<|#',#str) - (CHARINDEX('£#>',#str) + 3))

You can use REGEXP_SUBSTR to extract the numbers from your string.
For example,
SELECT REGEXP_SUBSTR('ShoeSize<|$à&£#>15<|#ç§~#>','[[:digit:]]+') FROM dual;
will return 15 to you. Of course, my assumption is that the shoe size in your string will be a number, and your intention is not to collate all digits which occur in your string to come at a number which you will save as shoe size

Related

SQL Server: How to select rows which contain value comprising of only one digit

I am trying to write a SQL query that only returns rows where a specific column (let's say 'amount' column) contains numbers comprising of only one digit, e.g. only '1's (1111111...) or only '2's (2222222...), etc.
In addition, 'amount' column contains numbers with decimal points as well and these kind of values should also be returned, e.g. 1111.11, 2222.22, etc
If you want to make the query generic that you don't have to specify each possible digit you could change the where to the following:
WHERE LEN(REPLACE(REPLACE(amount,LEFT(amount,1),''),'.','') = 0
This will always use the first digit as comparison for the rest of the string
If you are using SQL Server, then you can try this script:
SELECT *
FROM (
SELECT CAST(amount AS VARCHAR(30)) AS amount
FROM TableName
)t
WHERE LEN(REPLACE(REPLACE(amount,'1',''),'.','') = 0 OR
LEN(REPLACE(REPLACE(amount,'2',''),'.','') = 0
I tried like this in place of 1111111 replace with column name:
Select replace(Str(1111111, 12, 2),0,left(11111,1))

QTP, How to convert a decimal value (VarType = 14) to any other VarType like int, etc

I'm running the following query in UFT to retrieve a value from the database. It returns the correct number for me. The number is 21572939 but the vartype for the number is vbDecimal(14).
sqlMsgQuery = ("SELECT MAX(CONTNO)FROM SERIES A, CONTRACT B WHERE A.SERIES = B.SERIES AND B.SERIES = "&"'AAA-W'"&_
"AND A.PARTY_ROLE IS NULL AND B.CONTNO BETWEEN 21573931 AND 21574930")
On the other hand, i have another variable that holds the same value but this value is being retrieve from an oracle application and the vartype is vbString(8)
my job is to verify if the the two numbers are equal and then write it to datatable. I've tried a lot but failed to convert a decimal value with a vartype(14) to int and compare it with the other string that i've converted to int already.
What i'm looking for:
1. Either a query that will convert the number into int in the database before it is sent to the variable so that i can use it as int
2. Or a vbscript function/code that will do the conversion.
3. Or any other solution.
Thanks in advance.
If the length of the SQL column CONTNO is 8, you need to create a variable with VBString(8) as data type for storing the result from the SQL query .
And modify the existing query like below.
sqlMsgQuery = ("SELECT CAST(MAX(CONTNO) as VARCHAR (8))
FROM SERIES A, CONTRACT B
WHERE A.SERIES = B.SERIES AND B.SERIES = "&"'AAA-W'"&_
"AND A.PARTY_ROLE IS NULL AND B.CONTNO BETWEEN 21573931 AND 21574930")
You can change the required data to the data type you want using CAST in SQL.
CAST ( expression AS data_type [ ( length ) ] )
When you get the column CAST it as (data_type you want) then compare. Make sure both are same DATA_TYPE and same LENGTH.

SQL server casting string to integer checking value before casting

I have a table with a field named MINIMUM_AGE. The values stored in this field are of type nvarchar:
17 years
54 years
N/A
65 years
I would like to apply a WHERE clause on the column to check for a certain age range. To do that I need to parse out the age from the field values.
So, I think I need to select the first two characters, then cast them into an integer. Also, some fields may not contain numbers for the first two characters. Some may simply be N/A. So, I will need to check for that before casting.
Can someone explain how to accomplish this?
Here is the SQL Fiddle that demonstrates the below query:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
ELSE 0
END
FROM MyTable
Note: the CASE expression can only return one data type. So, in the example above if the MINIMUM_AGE is N/A then it returns 0.
If you would rather have it return null, then use the following:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
END
FROM MyTable

how to convert different datatypes to int in sql

I have a nvarchar(200) column in a table that contains a mix of integers (as strings) and non-integer strings and symbols also. E.g. Some sample data :-
Excuse me for my typing in my initial post I mentioned varchar(200) but in fact it is 'nvarchar(200)'
02
0
.../
125
00125
/2009
1000
0002589
000.00125
Marathi numbers like & letters
how can I order this Column?
You can use CAST to convert a varchar to an INT given that varchar is holding a proper number.
SELECT CAST(varCharCol as Int)
E.g.
col1 as Varchar(10)
col1 = '100' converting to INT will be successufl
but if col1 = '100 xyz' will be unsucessful in the process.
Looking at your string you may have to use number of LTRIM, REPLACE to get hold of the digits or using a regex to get comma separated numbers. That too it's not very clear how your original string looks like.
References.
Many DBMS have CAST() functions where you can convert one datatype to another.
For MySQL have a look at this site
You can Use CAST and Convert to convert string type value to int type. but be sure the value should numeric.
select convert(int,'123')
select CAST('123' as int)
You can use this query
SELECT CASE
WHEN ISNUMERIC(colName)=1 THEN ROUND(colName, 0)
ELSE 0 END AS [colName]
FROM tblName

determine DB2 text string length

I am trying to find out how to write an SQL statement that will grab fields where the string is not 12 characters long. I only want to grab the string if they are 10 characters.
What function can do this in DB2?
I figured it would be something like this, but I can't find anything on it.
select * from table where not length(fieldName, 12)
From similar question DB2 - find and compare the lentgh of the value in a table field - add RTRIM since LENGTH will return length of column definition. This should be correct:
select * from table where length(RTRIM(fieldName))=10
UPDATE 27.5.2019: maybe on older db2 versions the LENGTH function returned the length of column definition. On db2 10.5 I have tried the function and it returns data length, not column definition length:
select fieldname
, length(fieldName) len_only
, length(RTRIM(fieldName)) len_rtrim
from (values (cast('1234567890 ' as varchar(30)) ))
as tab(fieldName)
FIELDNAME LEN_ONLY LEN_RTRIM
------------------------------ ----------- -----------
1234567890 12 10
One can test this by using this term:
where length(fieldName)!=length(rtrim(fieldName))
This will grab records with strings (in the fieldName column) that are 10 characters long:
select * from table where length(fieldName)=10
Mostly we write below statement
select * from table where length(ltrim(rtrim(field)))=10;