2

I have a table with one column of strings, I want to create a new column with type string, and ` in front of each string item. How do I do that? example:

Old:

a
b
c

New:

"\`a"
"\`b"
"\`c"
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Deb
  • 35
  • 1
  • 6
  • If you like the answer from Thomas Smyth you presumably don’t want those backslashes in the result. – SJT Jan 05 '18 at 10:19

4 Answers4

4

Another solution (faster in this scenario):

q)update b:flip ("`";a) from tab
a b
------
a "`a"
b "`b"
c "`c"
terrylynch
  • 11,844
  • 13
  • 21
3

Take the following table as an example:

q)show tab:([]a:("a";"b";"c"))
a
-
a
b
c

To get a new column with the backtick in front you need to append "`" to each line:

q)update b:("`",'a) from tab
a b
------
a "`a"
b "`b"
c "`c"

If the column consists of symbols then it just needs to be converted to a string first:

q)tab2:([]a:`a`b`c)
q)update b:("`",'string a) from tab2
a b
------
a "`a"
b "`b"
c "`c"
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
1

Sometimes people, more familiar with SQL than with q, pose questions in terms of tables, when they need only handle vectors; and create new columns where all that is needed is a variable – or a value to pass as an argument.

q)"`",'tab[`a]
"`a"
"`b"
"`c"

Of course, the a column of tab is nothing but the vector "abc", so

q)"`",'"abc"
"`a"
"`b"
"`c"

might be all you need.

See:

SJT
  • 1,067
  • 5
  • 10
0

Here's another way: update ("`",/:a) from tab

Brian Rhee
  • 11
  • 1