sql Order by Query to display the name in order - sql

I am using oder by name in my sql query.
The following is the name in my table.
rama1
rama2
rama10
rama3
rama11
I am using the query to display the name order by name
the output is coming like,
rama1
rama10
rama11
rama2
rama3
But I need the output should be,
rama1
rama2
rama3
rama10
rama11
Please help me with the query to get the above output.
Thanks In advance

I suppose you have a wrong structure of your table. You should have a separate column, like ID of the numeric datatype, where you could keep your rama numeric part. In this case you would be able to make such queries without developing a bycicle.
In your case you can get numeric part from your string (see How to get the numeric part from a string using T-SQL? for ms sql) and order by it. But this is wrong way to go.

Try this
SELECT col FROM Table1
ORDER BY
CASE WHEN PatIndex('%[0-9]%',col) > 0
THEN RIGHT(col,LEN(col)- (PatIndex('%[0-9]%',col)-1)) * 1
ELSE col END
DEMO

Query:
SELECT t1.*
FROM Table1 t1
ORDER BY CAST(REPLACE(t1.col, 'rama', '') AS UNSIGNED) ASC
Result:
| COL |
----------
| rama1 |
| rama2 |
| rama3 |
| rama10 |
| rama11 |

Please try:
select *
From tbl
order by CAST(SUBSTRING(col, PATINDEX('%[0-9]%', col+'0'), 10) as int)

Related

Get Distinct value from a list in SQL Server

I have a DB column that has a comma delimited list:
VALUES ID
--------------------
1,11,32 A
11,12,28 B
1 C
32,12,1 D
When I run my SQL statement, in my WHERE clause I have tried IN, CONTAINS and LIKE with varying degrees of errors and success, but none offer an exact return of what I need.
What I need is a where clause that if I'm looking for all IDs with vale of '1' (NOT the number) in the list.
Example of problem:
WHERE values like (1)
This will return A,B,C,D because 1 is included in the value (11). I would expect IDs (A,C,D).
WHERE values like (2)
This will return A,B,D because 2 is included in the value (32,28,12). I would expect zeros records.
Thanks in advance for your help!
I will begin my answer by quoting the spot-on comment given by #Jarlh above:
Never, ever store data as comma separated items. It will only cause you lots of trouble.
That being said, if you're really stuck with this design, you could use:
SELECT *
FROM yourTable
WHERE ',' + [VALUES] + ',' LIKE '%,1,%';
The trick here is convert every VALUES into something looking like:
,11,12,28,
Then, we can search for a target number with comma delimiters on both sides. Since we placed commas at both ends, then every number in the CSV list is now guaranteed to have commas around it.
If you are stuck with such a poor data model, I would suggest:
select t.*
from t
where exists (select 1
from string_split(t.values, ',') s
where s.value = 1
);
Exactly i echo what jarlh and Tim says. relational model is not the right place to store comma delimited strings in table.
Here is an approach, that can likely use an index if there is one on column x
select distinct x
from t
cross apply string_split(t.x,',')
where value=1 /*out here you may parameterize, and also could make use of an index each if there is one in value*/
+---------+
| x |
+---------+
| 1 |
| 1,11,32 |
| 32,12,1 |
+---------+
working example
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b9b3084f52b0f42ffd17d90427016999
--SQL Server older versions
with data
as (
SELECT t.c.value('.', 'VARCHAR(1000)') as val
,y
,x
FROM (
SELECT x1 = CAST('<t>' +
REPLACE(x , ',', '</t><t>') + '</t>' AS XML)
,y
,x
FROM t
) a
CROSS APPLY x1.nodes('/t') t(c)
)
select x,y
from data
+---------+
| x |
+---------+
| 1 |
| 1,11,32 |
| 32,12,1 |
+---------+
working example
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=011a096bbdd759ea5fe3aa74b08bc895

How to retrieve records using comma separated values with IN clause?

I would like to retrieve certain records from a full list of table. Here I am using comma separated values with IN clause. The table rows looks like this:
Here is my SQL query, but the query completed with empty result set`
DECLARE #input VARCHAR(1000) = '2,3,17,10,16'
SELECT * FROM locations
WHERE
east_zone in (SELECT VALUE FROM string_split(#input,','))
OR
west_zone in (SELECT VALUE FROM string_split(#input,','))
Appreciate your help!
While this can be accomplished, i would request you to rethink your data model. Its a bad idea to store a comma separated list of ids/references in your databases. I strongly am with the comments of Tim Biegeleisen
Alternative would be store the list of zones-titles in a separate table.
Here is a way to accomplish this
with data
as (select 'model_check_holding' as col1,'1,2,3,4,5' as str union all
select 'model_cash_holding' as col1,'5,8,9' as str
)
,split_data
as (select *
from data
cross apply string_split(str,',')
)
,user_input
as(select '2,8,1' as input_val)
select *
from split_data
where value in (select x.value
from user_input
cross apply string_split(input_val,',') x
)
+---------------------+-----------+-------+
| col1 | str | value |
+---------------------+-----------+-------+
| model_check_holding | 1,2,3,4,5 | 1 |
| model_check_holding | 1,2,3,4,5 | 2 |
| model_cash_holding | 5,8,9 | 8 |
+---------------------+-----------+-------+
dbfiddle link
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=1cc9b224e443369744df19c1d7a7d789
Tim is 110% correct. Your data model is totally messed up -- not only storing multiple values in a delimited string. But also string numbers as strings. Wrong, wrong, wrong.
But if you are stuck with some else's really, really, really bad design choices, you do have an option:
DECLARE #input VARCHAR(1000) = '2,3,17,10,16';
SELECT l.*
FROM locations l
WHERE EXISTS (SELECT 1
FROM string_split(#input, ',') s1 JOIN
string_split(concat(l.east_zone, ',', l.west_zone), ',') l
ON s1.value = l.value
);
I do not recommend this approach. I merely suggest it as a stop-gap until you can fix the data model.

Extracting Multiple Numerical Values from Text

048(70F-Y),045(DDI-Y),454(CMDE-Y)
I have the above data in a column field, I need to extract each number before the, so in the above example I would want to see 048, 045, 454.
Note the data in the field will change in each record in the above you have 3 sets of numbers. Sometimes you may have just one set or 6 sets. I just need to capture all sets of numbers that are to the left of the (.
Ideally I would want the results to show in a new column like below. I have tried a few things and gotten no where any help would be greatly appreciate.
I would expect the result to look like the below:
+----------+-----------------------------------+---------------+
| EventId | PAEditTypes | Edits |
+----------+-----------------------------------+---------------+
| 6929107 | 082(SPA-Y),177(QL-Y) | 082, 177 |
| 26534980 | 048(70F-Y),045(DDI-Y),454(CMDE-Y) | 045, 048, 454 |
+----------+-----------------------------------+---------------+
You can get desired output with the following step:
use string_split with cross apply to isolate each item
use left to get only the first part of each item together with CHARINDEX to know where you have to stop
use STRING_AGG to build the final result, adding WITHIN GROUP clause to enforce ordering (if ordering is not important just remove WITHIN GROUP clause)
This is a TSQL sample that should work:
declare #tmp table ( EventId varchar(50), PAEditTypes varchar(200) )
insert into #tmp values
('6929107' ,'082(SPA-Y),177(QL-Y)' )
,('26534980','048(70F-Y),045(DDI-Y),454(CMDE-Y)')
select
EventId
, PAEditTypes
, STRING_AGG(left(value,CHARINDEX('(',value)-1),', ') WITHIN GROUP (ORDER BY value ASC) as Edits
from
#tmp
cross apply
string_split(PAEditTypes, ',')
group by
EventId
, PAEditTypes
order by
EventId desc
Output:

SQL query for two values of one row based off same table column

I have two columns of one row of a report that I would like to be based off the same one column in a SQL table.
For example, in the report it should be something like:
ID | Reason | SubReason
1 | Did not like | Appearance
In the SQL table it is something like:
ID | ReturnReason
1 | Did not like
1 | XX*SR*Appearance
1 | XX - TestData
1 | XX - TestData2
The SubReason column is being newly added and the current SQL query is something like:
SELECT ID, ReturnReason AS 'Reason'
FROM table
WHERE LEFT(ReturnReason,2) NOT IN ('XX')
And now I'd like to add a column in the SELECT statement for SubReason, which should be the value if *SR* is in the value. This however won't work because it also has 'XX' in the value, which is omitted by the current WHERE clause.
SELECT t.ID, t.ReturnReason AS 'Reason',
SUBSTRING(t1.ReturnReason,7,10000) as 'SubReason '
FROM t
LEFT JOIN t as t1 on t.id=t1.id and t1.ReturnReason LIKE 'XX*SR*%'
WHERE t.ReturnReason NOT LIKE 'XX%'
SQLFiddle demo

how to select one tuple in rows based on variable field value

I'm quite new into SQL and I'd like to make a SELECT statement to retrieve only the first row of a set base on a column value. I'll try to make it clearer with a table example.
Here is my table data :
chip_id | sample_id
-------------------
1 | 45
1 | 55
1 | 5986
2 | 453
2 | 12
3 | 4567
3 | 9
I'd like to have a SELECT statement that fetch the first line with chip_id=1,2,3
Like this :
chip_id | sample_id
-------------------
1 | 45 or 55 or whatever
2 | 12 or 453 ...
3 | 9 or ...
How can I do this?
Thanks
i'd probably:
set a variable =0
order your table by chip_id
read the table in row by row
if table[row]>variable, store the table[row] in a result array,increment variable
loop till done
return your result array
though depending on your DB,query and versions you'll probably get unpredictable/unreliable returns.
You can get one value using row_number():
select chip_id, sample_id
from (select chip_id, sample_id,
row_number() over (partition by chip_id order by rand()) as seqnum
) t
where seqnum = 1
This returns a random value. In SQL, tables are inherently unordered, so there is no concept of "first". You need an auto incrementing id or creation date or some way of defining "first" to get the "first".
If you have such a column, then replace rand() with the column.
Provided I understood your output, if you are using PostGreSQL 9, you can use this:
SELECT chip_id ,
string_agg(sample_id, ' or ')
FROM your_table
GROUP BY chip_id
You need to group your data with a GROUP BY query.
When you group, generally you want the max, the min, or some other values to represent your group. You can do sums, count, all kind of group operations.
For your example, you don't seem to want a specific group operation, so the query could be as simple as this one :
SELECT chip_id, MAX(sample_id)
FROM table
GROUP BY chip_id
This way you are retrieving the maximum sample_id for each of the chip_id.