Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The records (with delimiter) in my Database look like this:
100
100/1
100/2
101
101/1
102
102/1
102/2

My problem is to find a way to select the last values from each record and bind them to a Gridview (vb.net).
I want the output to look like the following:
100/2
101/1
102/2 ..

Regards
Anas
Posted
Updated 10-Mar-14 5:51am
v3
Comments
Rage 10-Mar-14 9:18am    
Databases are not my cup of tea, but what's wrong in fetching these records with a SQL statement ?
What have you tried and where is the problem?
Member 10529245 10-Mar-14 20:44pm    
Thanks for your effort... my problem is need to retrieve all the maximum value from each record in mysql... from that above example i want the out put will be
1.100/2
2.101/1
3.102/2 how to retrieve all the rows based on this maximum value .. that max row has been going to bind in Gridview ....

Finally i find the Solution that is

SELECT col1,col2,col3 FROM Tableone WHERE
Id=(SELECT MAX(ID) FROM Tableone i WHERE i.empname=Tableone.Empname)
 
Share this answer
 
Alternativelly, you can use REPLACE[^] function.

SQL
DECLARE @tmp TABLE (StringNumbers VARCHAR(10))

INSERT INTO @tmp (StringNumbers)
VALUES('100'), ('100/1'),
('100/2'), ('101'),
('101/1'), ('102'),
('102/1'), ('102/2'),
('103'), ('104')


SELECT StringNumbers, CONVERT(FLOAT, REPLACE(StringNumbers ,'/','.')) AS MyNumber
FROM @tmp
ORDER BY CONVERT(FLOAT, REPLACE(StringNumbers ,'/','.'))


Think of it and change the code to your needs ;)
 
Share this answer
 
The problem with that is that "100/1" is a string based value, so it will work with a string based comparison, rather than numeric. You can do it, but it's pretty cumbersome in SQL - I'd do it in a "proper" programing language where it's pretty simple.

SQL
SELECT MAX(ID) FROM MyTable
GROUP BY SUBSTRING(ID, 0, CASE WHEN CHARINDEX('/', ID) > 0
                               THEN CHARINDEX('/', ID)
                               ELSE LEN(ID)
                          END)


If you can change your DB so it's two fields, it becomes a lot easier!
 
Share this answer
 
Comments
Maciej Los 10-Mar-14 14:59pm    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900