SQL extract multiple numeric values from string column [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I read and used the function suggested in this article:
http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/
and it works nice but actually I need the result in two columns (I'm expecting two numbers that should be seperated with spaces and / char in the original string)
Let's say "nfsdklj6 / 3 ddfsdf" should be 6 | 3
"nfsdklj45 / 100 ddfsdf" should be 45 | 100
I'm a rookie sql programmer, so any help or hint would be appreciated,
thanks

If you are using the mentioned udf
Declare #Table table (MyString varchar(25))
Insert into #Table (MyString) values
('nfsdklj6 / 3 ddfsdf'),
('nfsdklj45 / 100 ddfsdf')
Select *
,Val1=dbo.udf_GetNumeric(substring(MyString,1,charindex('/',MyString)))
,Val2=dbo.udf_GetNumeric(substring(MyString,charindex('/',MyString),50))
From #Table
Returns
MyString Val1 Val2
nfsdklj6 / 3 ddfsdf 6 3
nfsdklj45 / 100 ddfsdf 45 100
For your specific table
Select Shuma
,Val1=dbo.udf_GetNumeric(substring(Shuma,1,charindex('/',Shuma)))
,Val2=dbo.udf_GetNumeric(substring(Shuma,charindex('/',Shuma),50))
From [dbo],[Deals]
Where Shuma not like LIKE '%1 / 1%'

Related

SQL query to return only 11 digit numeric values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Actual data:
|31345678921|
|56789056789|
|56780345678|
|4567 |
|3456 |
|00678596 |
|03456788453|
|ASA 2344 |
|34565 |
|BBq23 |
|DNF LIMIT |
Required data:
|31345678921|
|56789056789|
|56780345678|
|03456788453|
One method is:
where actual not like '%[^0-9]%' and
len(actual) = 11
That is, the value has no non-digits and is 11 digits long.
Another method is:
where try_convert(numeric(11,0), actual) between 10000000000 and 99999999999
That is, when converted to a numeric, the range suggests that it is 11 digits.
And a third method is:
where actual like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
SQL Server supports character classes with like, so you can just like [0-9] 11 times.

SQL select statement to Exclude a column from being sorted [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help
This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name

How to get avg between two values in SQL Server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Please refer to the details shown here:
ID Value
---------
1 120
1 150
Query :
Select avg(value)
from table
group by Id
Current output = 130
Expected output =
120 + 150 / 2 = 135
Please let me know your comments.
SQL Server does integer arithmetic, even for integers. However, it doesn't round integers to the nearest 10. Perhaps on your real data, the following will do what you want:
Select avg(value * 1.0)
from table
group by Id ;
It changes the value to something with a decimal point, so the average is not an integer average.

Assign values to sorted result in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo

Remove trailing zeros from a decimal column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How can I remove the trailing zero from the below table using SQL:
SKU SIZE
ABC 35.000000
QWE 36.000000
RTY 37.000000
VGY 38.000000
Expected results:
SKU SIZE
ABC 35
QWE 36
RTY 37
VGY 38
In sql server cast the column containing decimal values to Int
Select SKU,CAST (Size AS INT)
From tablename
For MySql use integer division function DIV
SELECT SKU,Size DIV 1
From Tablename
I suggest
SELECT sku, int(size)
FROM tablename
It's less typing. ;-)