Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello, is there a SQL function or statement to get a value of a table with row and column specified? Googling came out with nothing usable.
Thanks in advance.
Posted
Comments
Magic Wonder 15-Sep-14 3:36am    
Amazed to know that Google failed with your expectation...!!!
George Jonsson 15-Sep-14 3:36am    
How to identify the row? Via a row number or by a value?
YourAverageCoder 15-Sep-14 3:37am    
Value but number would be nice too.
george4986 15-Sep-14 6:01am    
a sample solution is added for reference

As you don't specify any columns only a generic SELECT statement can be given for the value case

SQL
SELECT specific_column_name
FROM table_name
WHERE another_column_name operator value;

For this to work, the values in the another_column_name must be unique.

Another option is to use a row number.
This would work in MySQL (you don't tell which database you use either)
SQL
SELECT specific_column_name 
from table_name
limit 10, 1; -- Get value from row 10


For SQL Server see this link ROW_NUMBER (Transact-SQL)[^]
 
Share this answer
 
SQL
-----------sample table structure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[area](
	[area_id] [bigint] NOT NULL,
	[area_code] [varchar](10) NULL,
	[area_name] [varchar](50) NOT NULL,
	[latitude] [float] NOT NULL,
	[longitude] [float] NOT NULL,
 CONSTRAINT [PK_area] PRIMARY KEY CLUSTERED 
(
	[area_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[area] ([area_id], [area_code], [area_name], [latitude], [longitude]) VALUES (1, N'EKM', N'Ernakulam', 9.97768, 76.29684)
INSERT [dbo].[area] ([area_id], [area_code], [area_name], [latitude], [longitude]) VALUES (2, N'TCR', N'Thrissur', 10.5243, 76.2125)

-------------------------getting (x,y) record from table--------------------------------
declare @inpXAxis INT
declare @inpYAxis INT
declare @reqColXAxis as varchar(100)
declare @reqColYAxis as varchar(100)
declare @sq1Query as varchar(MAX) 
declare @tableName as varchar(100)

---give table name here
SET @tableName='area'
---give X axis here , column no  
SET @inpXAxis=3
----give Y axis here, row no      
SET @inpYAxis=2

---get column name
select @reqColXAxis = column_name from information_schema.columns where table_name = @tableName 
and ordinal_position = @inpXAxis
----drop temp table if exists
	IF OBJECT_ID('tempdb..#temp1') IS NOT NULL
			BEGIN
				drop table #temp1
			END	

---fetch column having all rows along with a new index (myRow) into temp table temp1,also provide row no: as the second query
set @sq1Query = 'select IDENTITY(int,1,1) as myRow, ' + @reqColXAxis+' INTO #temp1  from '+@tableName +' ; SELECT '+@reqColXAxis +' FROM #temp1 WHERE myRow = '+CAST ( @inpYAxis as varchar)
exec(@sq1Query)


good luck ;-)
 
Share this answer
 
Comments
YourAverageCoder 15-Sep-14 6:35am    
You did awesome. Much thanks.
george4986 15-Sep-14 6:44am    
u r always welcome ;-)
Hi,

Check this...


SQL
Select * from yourTable


above will give output as per your subject line.


Hope this will help you.

Cheers
 
Share this answer
 
Comments
YourAverageCoder 15-Sep-14 3:36am    
That's too generic. I need more specific stuff. The best select can offer is a specified column while i need specified row too.
Magic Wonder 15-Sep-14 3:39am    
If you will not share your " more specific stuff ", then how can one help you out?
Thanks7872 15-Sep-14 3:49am    
I think OP is confused with what he wants to do. All in all, dynamic query is what he is looking for, I suspect.
Magic Wonder 15-Sep-14 3:56am    
May be.
Thanks7872 15-Sep-14 3:48am    
Do you know what dynamic query is? If not, search google for that. To what you have stated in this comment,Do you know what where clause does?For specific row you can apply where clause.

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