How to select last colum only from database? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to select last colum only from database?

19th Feb 2018, 7:44 PM
Sajin Surya G
Sajin Surya G - avatar
2 Answers
+ 6
SELECT <last_field_name> FROM <table_name> will do that, though you will need to know the 'last_field_name' to do it, or are you asking for last record from the table? to get the last record use SELECT with either LIMIT or TOP (depending on DBMS), then add sort ordering ORDER BY DESC. Hth, cmiiw
19th Feb 2018, 8:23 PM
Ipang
+ 1
Hmm it's a strange question but... For example in MS SQL you are able to get last column from the table using something like this: Declare @ColumnName VARCHAR(100) set @ColumnName=(SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA='YOUR TABLE SCHEMA' and TABLE_NAME='YOUR TABLE NAME' Order by ORDINAL_POSITION desc) Declare @sql nvarchar(max) set @sql='select ' + @ColumnName + ' from YOUR_TABLE_SCHEMA.YOUR_TABLE_NAME' EXEC sp_sqlexec @sql but I have no idea why someone could need it.
20th Feb 2018, 11:10 AM
Pawko
Pawko - avatar