1

I'm trying to concatenate a string:

concat(product," (",cast(created_date as string)," - ",cast(end_date as string),") ")

in Output I get :

Row           String
1       Product_B (12-02-2020 - 21-04-2020)
2       Product_A (10-01-2020 - 11-02-2020)
3       Product_C (14-05-2020 - 19-07-2020)

So I want to add a line break in concatenate string And as a result, receive:

Row              String
1               Product_B 
           (12-02-2020 - 21-04-2020)
2               Product_A
           (10-02-2020 - 11-02-2020)
3               Product_C  
           (14-05-2020 - 19-07-2020)

What needs to be added to get break a string line?

2 Answers 2

3

Consider below option

select *,
  format('%s\n(%s - %s)', product, created_date, end_date) rip
from `project.dataset.table`

if applied to sample data in your question -

with `project.dataset.table` as (                
  select 'Product_B' product, '2020-02-12' created_date, '2020-04-21' end_date union all
  select 'Product_A', '2020-01-10', '2020-02-11' union all
  select 'Product_C', '2020-05-14', '2020-07-19' 
)       

output is

enter image description here

In case if your dates column are of DATE data type - use below instead

select *,
  format('%s\n(%t - %t)', product, created_date, end_date) rip
from `project.dataset.table` 
3

Try \n:

select concat("a","\n","c")

or

select "a\nc"

enter image description here

1
  • Thanks Sergey. To extend your answer you can use \n\n to generate a blank line between the strings.
    – CJC
    Commented Feb 23, 2024 at 14:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.