How to get an array with column names of a table
Suppose you have a Post model:
Post.column_names
# or
Post.columns.map { |column| column.name }
It will return an array with column names of the table 'posts'.
Create a new column with an array of column names based on condition
Consider below approach
select *,
regexp_extract_all(translate(to_json_string(t), '{}"', ''), r'(\w+):1') as Array_col
from mytable t
if applied to sample data in your question - output is
Create array with column names in BigQuery
You should be able to achieve what you want with the following:
DECLARE column_names ARRAY <STRING>
SET column_names = (SELECT arraY_agg(column_name) FROM Books1.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1'
);
Adding array_agg
makes the query return an array of the column names.
Creating an array of column names in Microsoft Server SQL
SELECT COLUMN_NAME FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY ORDINAL_POSITION) Num, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'tablename'
) A
WHERE NUM = 1
ORDINAL_POSITION
is nothing but "column identification number".
How to get table column names as array from model
You can use the getColumnListing()
method:
use Schema;
Schema::getColumnListing($this->getTable())
JScript - Array with columns from table
You could do something like this using vanilla javascript
let columnData = Array.from(table.rows).
slice(1, table.rows.length).
map(row =>Array.from(row.cells).map(x => x.innerText)).
reduce((acc,rowData)=>{
rowData.forEach((value,index)=>{
acc[index]= acc[index] || [ ]
acc[index].push(value)
})
return acc },[])
let table = document.getElementById('tab')debuggerlet headers = Array.from(table.rows[0].cells).map(x => x.innerText)let columnData = Array.from(table.rows). slice(1, table.rows.length). map(row =>Array.from(row.cells).map(x => x.innerText)) .reduce((acc,rowData)=>{ rowData.forEach((value,index)=>{ acc[index]= acc[index] || [ ] acc[index].push(value) }) return acc },[])console.log(headers)console.log(columnData)
<table id="tab"> <tr> <th> Name </th> <th> Age </th> <th> Location </th> </tr>
<tr> <th> Jason </th> <th> 22 </th> <th> Texas </th> </tr> <tr> <th> Lawson </th> <th> 21 </th> <th> Florida </th> </tr> <tr> <th> Jose </th> <th> 25 </th> <th> London </th> </tr>
</table>
How to store the column names of a table in an array in Snowflake
Check Mike Walton's solution that doesn't use stored procedures. Leaving this answer up for anyone looking for an answer with stored procedures.
One solution is to run a SELECT *
within the stored procedure, and then the statement will have the name of the columns:
CREATE OR REPLACE PROCEDURE get_columns(TABLE_NAME VARCHAR)
RETURNS ARRAY
LANGUAGE JAVASCRIPT
AS
$$
var stmt = snowflake.createStatement({
sqlText: "SELECT * FROM " + TABLE_NAME + " LIMIT 1;",
});
stmt.execute();
var cols=[];
for (i = 1; i <= stmt.getColumnCount(); i++) {
cols.push(stmt.getColumnName(i));
}
return cols
$$
;
Related Topics
How to Get the Width of Terminal Window in Ruby
What Is the Easiest Way I Can Create a 'Beep' Sound from a Ruby Program
Vcrproxy: Record Phantomjs Ajax Calls with Vcr Inside Capybara
Bundle Install Could Not Fetch Specs from Https://Rubygems.Org/
Creating Categories on Jekyll Driven Site
How to Remove/Disable Sign Up from Devise
How to Make Sign Up Page Be Root Page in Devise
Rails -- Create and Create! Methods, Ror 3 Tutorial
Converting an Empty String into Nil in Ruby
Gem Dependencies Versions Meaning
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby
How to Check What Is Queued in Activejob Using Rspec
Ruby Concatenate Strings and Add Spaces
How to Check If a Gem Is Installed
How Are Symbols Used to Identify Arguments in Ruby Methods
Simple_Form: Remove Outer Label for an Inline Checkbox with Label
Rails Scope Find with Current User
Ruby: How to Pass All Parameters and Blocks Received by One Method to Another