I want to extract numeric value from a varchar in SQL Server 2014
XXXX 0.5Kg I want to return 0.5
XXX 25YY 1.25Kg I want to return 1.25
I want the number between last space from left and Kg
Thanks
You could do
select replace(right(str, charindex(' ',reverse(str))),'Kg','')
from your_table;
DECLARE #string as VARCHAR(MAX) = 'XXX 25YY 1.25Kg'
DECLARE #string_ as VARCHAR(MAX) = 'XXXX 0.5Kg'
SELECT REPLACE(RIGHT(#string, CHARINDEX(' ',REVERSE(#string))),'Kg','')
SELECT REPLACE(RIGHT(#string_, CHARINDEX(' ',REVERSE(#string_))),'Kg','')
First find out the first space in the reverse order of the string and then break the string from that space. the replace the 'kg' from '' to obtain the value that you need. Test the result with many strings. You can use RIGHT string function in SQL to get the specific part from the string.
Related
I have a table called transactions which has a column Customer_Transactions_ID
Now the values in this column vary in the below
AB_EL_205_72330_H
AB_EL_820_23066_E_N
AB_EL_820_23066
I want to trim all the values that start with "AB_EL" to make them from
AB_EL_820_23066_E_N (or whatever variation of the above) to
AB_EL_820_23066
So basically no E or H after the id's
Is this possible?
This will give your values truncated until the last ocurrence of a number.
SELECT
Original = T.Customer_Transactions_ID,
Truncated = SUBSTRING(
T.Customer_Transactions_ID,
1,
LEN(T.Customer_Transactions_ID) - PATINDEX('%[0-9]%', REVERSE(T.Customer_Transactions_ID)) + 1)
FROM
Transactions AS T
if i understand your requirement correctly.if text starts with ab_el then trim _e,_l from the text.
DECLARE #txt VARCHAR(100)= 'aqb_el_205_72330_h';
SELECT ISNULL('ab_el'+CASE
WHEN #txt LIKE 'ab_el%'
THEN replace(replace(REPLACE(#txt,'ab_el',''), '_e', ''), '_h', '')
END,#txt);
I have a string like this 'sdf,11-df,12-asd,sadfsdf'. But I need only numbers from this string in multiple rows like this.
11
12
I need numbers between (,) and (-) that's the actual requirement. String can be like 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd' anything, But numbers will always between (,) and (-)
or find numbers between (,) and (-) it will also help me.
Thanks in advance.
I need any solution guys please help,
Following will work with SQL Server 2016 or higher:
DECLARE #str varchar(50) = 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd'
SELECT
LEFT(Value, CHARINDEX('-', Value)-1)
FROM STRING_SPLIT(#str, ',')
WHERE PATINDEX('%[0-9]-%',Value) > 0
try this
DECLARE #data nvarchar(max) = 'sdf,11-df,12-asd,sadfsdf'
select substring(#data,PATINDEX('%[0-9]%',#data),2)
union all
select substring(#data,PATINDEX('%[-]%',REVERSE(#data))-1,2)
Update
You can also use a function which returns as a table
CREATE FUNCTION dbo.udf_GetNumeric (#strAlphaNumeric VARCHAR(256))
RETURNS TABLE AS RETURN ( select LEFT(value,2) as Value from
string_split(#strAlphaNumeric,',') where PATINDEX('%[0-9]-%',value)
> 0 )
END GO
SELECT * from
dbo.udf_GetNumeric('sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd')
I have a string which is in 99999 124 fdsg format.
I want to replace the first space with - and the next space with a non-space. My expected result will look like this:
99999-124fdsg
To replace the first char I used this expression:
stuff(Product,charindex('',product),1,'-')as trim
Because I want to name the new column as trim. How can I do for the second character?
Charindex has last parameter start_location use it to search for second empty string
Try this
DECLARE #str VARCHAR(50) = '99999 124 fdsg'
SELECT Stuff(Stuff(#str,Charindex(' ',#str),1,'-'),Charindex(' ',#str,Charindex(' ',#str) + 1),1,'')
Result : 99999-124fdsg
Assuming, that you example with STUFF works correctly for the first blank you can just wrap this with REPLACE. After STUFF there's only the other blank left:
replace(stuff(Product,charindex(' ',product),1,'-'),' ','')
Working example:
DECLARE #s VARCHAR(50) = '99999 124 fdsg';
select replace(stuff(#s,charindex(' ',#s),1,'-'),' ','')
The result
99999-124fdsg
I want to convert data from Varchar column to Numeric data type -
so before conversion I am calling ISNUMERIC function to check whether data is numeric and if yes convert it to numeric data type. But I am facing an issue -
IsNumeric function is not behaving as expected -
- IsNumeric(x) returns true - when varchar value has both comma and a tab character (Char(9)),
- IsNumeric(x) returns false - when varchar value has only tab character and no comma
It is explained with the help of below SQL -
DECLARE #propValue AS VARCHAR(50)
SET #propValue = '1,592 ' -- contains comma + tab (Char(9))
SELECT ISNUMERIC(#propValue) -- Returns 1
--If ISNUMERIC func returns true for this value, lets convert this Varchar value to Numeric
SELECT CAST(#propValue AS Numeric(19, 4)) -- :-( Error converting data type varchar to numeric.
I Googled and found various solutions to tackle this problem -
--Solution 1: use 'e0'
SELECT ISNUMERIC(#propValue + 'e0') -- Returns 0
--Solution 2: remove comma before calling IsNumeric()
SELECT ISNUMERIC(REPLACE(#propValue, ',', '')) -- Returns 0
--Solution 3
--Call CLR function to parse Varchar value
What is the recommended solution in above scenario and why?
Also, I would really appreciate if anyone can explain why IsNumeric(x) returns false - when varchar value has only tab character and no comma?
Thank you!
Keep in mind that ISNUMERIC() = 1 does not mean "can be converted to every numeric type" but rather "can be converted to at least one numeric type." This comes up a lot where the value can be converted to at least one numeric type, but not the one you want. A much more basic example:
IF ISNUMERIC(256) = 1
SELECT CONVERT(TINYINT, 256);
If you have strings that have known violations (such as tabs), then why not also replace tabs? Why not prevent garbage data from getting into this value in the first place?
This works but it is quite ugly:
DECLARE #x TABLE (propValue VARCHAR(50));
INSERT #x SELECT '1,592' + CHAR(9)
UNION ALL SELECT '55' + CHAR(9) + '32'
UNION ALL SELECT CHAR(9) + '7,452.32 '
UNION ALL SELECT 'foo'
UNION ALL SELECT '74';
SELECT CONVERT(NUMERIC(19,4),
LTRIM(RTRIM(REPLACE(REPLACE(propValue, CHAR(9), ''), ',', '')))
)
FROM #x
WHERE ISNUMERIC(LTRIM(RTRIM(REPLACE(REPLACE(propValue,
CHAR(9), ''), ',', ''))) + 'e0') = 1;
While not applicable to your exact question, I wrote a FAQ about this 10 years ago: http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html
ISNUMERIC(RTRIM(LTRIM(x))) = 1
I have a vairable
DECLARE #AssignOn nvarchar(20)='0,2,5'
I want to check a condition like this
DECLARE #index int
SET DATEFIRST 7
SELECT #index=DATEPART(DW, GETDATE())-1
IF(CONVERT(nvarchar(2),#index) IN #AssignOn)
IN cannot be used here . Any other methods to do this INLINE
You can use CharIndex to find if you have a match. It returns a non zero value if the first string appears in the second.
IF(CHARINDEX(CONVERT(nvarchar(2),#index), #AssignOn) > 0)
The easiest way to do this is to search for the substring ',needle,' in the csv list string. However, this doesn't work correctly for the first and last elements. This can be overcome by concatenating a comma onto each side of the csv list string.
An example in SQL might be:
SELECT
CHARINDEX(','+ NEEDLE +',', ','+ HAYSTACK +',')
FROM table;
Or using LIKE:
SELECT *
FROM table
WHERE ','+ HAYSTACK +',' LIKE '%,'+ NEEDLE +',';
IF CHARINDEX(','+CONVERT(nvarchar(2),#index)+',', ','+#AssignOn+',') <> 0
As you actually define the values in the code you could instead;
DECLARE #AssignOn TABLE (value int)
INSERT #AssignOn VALUES (0),(2),(5)
... #index IN (SELECT value FROM #AssignOn)