Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL string value spanning multiple lines in query

Tags:

string

sql

sqlite

UPDATE: the bio may contain apostrophes (see updated example)

I have an SQL query that has a value which spans multiple lines and it causes the query to fail:

UPDATE User SET UserId=12345, Name="J Doe", Location="USA", Bio="I'm a bio that has an apostrophe, and I'm  spanning multiple lines!"  WHERE UserId=12345 

In C# you can put an @ before the string Bio=@"..." in order to allow it to span multiple lines, but I'm not sure how the same thing can be achieved with SQL queries. How do you get a string to span multiple lines without having to do things like manually concatenating the strings:

Bio="I'm a" +" bio that has an apostrophe, and I'm" +" spanning multiple lines!"  
like image 467
Kiril Avatar asked Nov 16 '11 03:11

Kiril


People also ask

How do you do multiple lines in SQL?

Multi-line comments start with /* and end with */ . Any text between /* and */ will be ignored.

How split a string in SQL query?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument. FROM STRING_SPLIT( 'An example sentence.

What is {} in SQL query?

The curly braces are for complex variable expressions. They are interpreted by PHP, not by the SQL interface. $query = "SELECT * FROM users WHERE user='$_POST['username']' AND password='$_POST['password']'"; The above will lead to an parsing error.

Can you use line break in SQL?

We might require inserting a carriage return or line break while working with the string data. In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break.


1 Answers

SQL Server allows the following (be careful to use single quotes instead of double)

UPDATE User SET UserId = 12345    , Name = 'J Doe'    , Location = 'USA'    , Bio='my bio spans  multiple lines!' WHERE UserId = 12345 
like image 70
Adam Wenger Avatar answered Sep 25 '22 08:09

Adam Wenger