BigQuery Set column name with variable - google-bigquery

I would like to set column name with variable (or with SELECT query). I tried below which does not work.
DECLARE colname STRING DEFAULT "name";
SELECT "value" as colname
Returns wrong column name (colname instead of name):
colname
value
and
DECLARE colname STRING DEFAULT "name";
SELECT "value" as (SELECT colname);
Returns Error
Neither worked. Is there any way to do this?
The result should be:
name
value
I have a bigger problem, that I want to solve with this.

Try this
DECLARE colname STRING DEFAULT "name";
execute immediate "select 'value' as "|| colname

Related

SQL / DB2 Array in Where declared in with

anyone does know a workaround for querying with parameters/variables for usage in where [...] in functions on db2 v11?
what i tried:
DECLARE #list varchar(23) = '1,2,3,4'
SELECT ...FROM tbl WHERE col IN (#list)
WITH test(val) AS (VALUES(ARRAY['5','9']))
SELECT ... FROM table, test WHERE col ANY(val)
both do not work, first one isn't db2 compatible, the second ones does not work cause he cant split the values.
any ideas or examples?
Try this:
SELECT t.*
FROM tbl t
WHERE EXISTS
(
select 1
from xmltable
(
'for $id in tokenize($s, ",") return <i>{string($id)}</i>'
passing '1,2,3,4' as "s"
columns
tok int path '.'
) v
where v.tok = t.col
);
You may use a parameter marker instead of the string constant 1,2,3,4 as for usual string paramter, if you want to provide such a list of integers as a comma separated string at runtime.
Declare local temporary table and insert your value list in that ?

SQL Replace only a part of a path

I have a table (about 160k rows) with a column called paths.
In that column there are paths like:
"\\ab.local\folder1\folder2\folder3\folder_x"
"\\ab.local\folderA\"
The length of the paths differ.
What I would like to do is to replace only the "ab" before the .local in to "cd" and leave the rest untouched.
I have been told to use a replace function, but somehow I don't get it to work the way I want to.
I am looking for the right syntax to do this.
Declare #oldval as varchar(30) = '\\ab.local\' ;
Declare #newval as varchar(30) = '\\cd.local\' ;
update yourtable set yourfield = replace(yourfield,#oldval ,#newval) where yourfield like #oldval + '%'
Solved Reference : https://stackoverflow.com/a/814551/6923146
Hope it's perfect for your solution
UPDATE my_table
SET columnName = replace(columnName, 'oldstring', 'newstring')
WHERE columnName like '%oldstring%'
For example:
UPDATE my_table
SET columnName = replace(columnName, '\ab.', '\ab.')
WHERE columnName like '%\ab.%'
If the part to replace is on a fixed position with a fixed length you could use STUFF, like so:
UPDATE yourTable SET paths = STUFF(paths, 3, 2, 'cd')
This replaces two characters in paths beginning at position 3 with cd

How to convert string set to hiveconf variable to an object usable as part of a table name

I am looking for a way to remove quotes from a hiveconf variable string, so that I can use it also as part of a table name:
Basicaly, I have something like
set sub_name = "123";
select ${hiveconf:sub_name} from table_${hiveconf:sub_name};
And when executing I need it to work like:
select "123" from table_123;
For that, I could run with something like:
set variable = "123";
set table_subname = 123;
select ${hiveconf:variable} from table_${hiveconf:table_subname};
Which would then work as
select "123" from table_123;
But is there some elegant way how to use just the one variable, once as a string and once as a part of the table name?
hive> create table table_abc as select 'X' as x;
OK
x
hive> set sub_name=abc;
hive> select "${hiveconf:sub_name}" from table_${hiveconf:sub_name};
OK
_c0
abc

T-SQL input parameter as stringArray()

I am writing update query
"Update tbl_List Set ListName = 'Hello' where ListId IN (stringArray()).
I have added 5 string values in stringArray() and how to pass it as input parameter to SQL ? How to execute query with Array values?
You can create your query as :
Update tbl_List Set ListName = 'Hello' where ListId IN ('value1' , 'value2' ,'valueN');
Or using a table :
Declare #Val Table (IDs Nvarchar(50) );
Insert into #val values ('value1'),('value2'),('valueN');
Update tbl_List Set ListName = 'Hello' where ListId IN (Select IDs From #Val);
You can loop in your StringArray to pass values , and you can use only one parametre and excute your query as much as the length of your Array.
Also you can use temp tables for that job.

How to replace a string in a SQL Server Table Column

I have a table (SQL Sever) which references paths (UNC or otherwise), but now the path is going to change.
In the path column, I have many records and I need to change just a portion of the path, but not the entire path. And I need to change the same string to the new one, in every record.
How can I do this with a simple update?
It's this easy:
update my_table
set path = replace(path, 'oldstring', 'newstring')
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
I tried the above but it did not yield the correct result. The following one does:
update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'
UPDATE CustomReports_Ta
SET vchFilter = REPLACE(CAST(vchFilter AS nvarchar(max)), '\\Ingl-report\Templates', 'C:\Customer_Templates')
where CAST(vchFilter AS nvarchar(max)) LIKE '%\\Ingl-report\Templates%'
Without the CAST function I got an error
Argument data type ntext is invalid for argument 1 of replace function.
You can use this query
update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
all answers are great but I just want to give you a good example
select replace('this value from table', 'table', 'table but updated')
this SQL statement will replace the existence of the word "table"
(second parameter) inside the given statement(first parameter) with the third parameter
the initial value is this value from table but after executing replace function it will be this value from table but updated
and here is a real example
UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'
for example if we have this value
10.7440/perifrasis.2010.1.issue-1
it will become
10.25025/perifrasis.2010.1.issue-1
hope this gives you better visualization
select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable
where "ImagePath" is my column Name. "NewImagePath" is temporery
column Name insted of "ImagePath" "~/" is my current string.(old
string) "../" is my requried string.(new string)
"tblMyTable" is my table in database.
you need to replace path with the help of replace function.
update table_name set column_name = replace(column_name, 'oldstring', 'newstring')
here column_name refers to that column which you want to change.
Hope it will work.
If target column type is other than varchar/nvarchar like text, we need to cast the column value as string and then convert it as:
update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'
You also can replace large text for email template at run time, here is an simple example for that.
DECLARE #xml NVARCHAR(MAX)
SET #xml = CAST((SELECT [column] AS 'td','',
,[StartDate] AS 'td'
FROM [table]
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
select REPLACE((EmailTemplate), '[#xml]', #xml) as Newtemplate
FROM [dbo].[template] where id = 1