new line in SQL insert query - sql

I want to use the following statement to add a row in a table named people with one column which is varchar(100)
insert into people values ('a b')
I want to have a new line between a and b.
How to do this?
I tried something like 'a CHAR(13) b' but did not work.
Thanks.

How about
'a ' + CHAR(13) + ' b'

This works as well:
'a' + CRLF + 'b'
Check this link as well
How to insert a line break in a SQL Server VARCHAR/NVARCHAR string

Related

How to add commas to a value?

Is it possible to add to the result value inside a column commas?
I mean for example I create new value:
insert into dbo.SAPID (TEST2)
Values (110)
I want the value to be with commas='110' inside the column result set.
insert into dbo.SAPID (TEST2)
Values (char(39) + '110' + char(39))
Please clarify your request. It seems like TEST2 is an Integer and you would like to return a string with single quotes surrounding it.
To do this you can cast the value when selected and append the quote:
SELECT '''' + CAST( TEST2 AS NVARCHAR(10) ) + '''' FROM SAPID
I believe you are thinking of single quotes '' like you provided in your example above:
I want the value to be with commas='110'
In that case, you could just do something like this:
--your column in the table will need to be a string instead of int
create table dbo.SAPID (TEST2 nvarchar(10));
--insert string value with single quotes
insert into dbo.SAPID(TEST2)
VALUES ('''' + '110' + '''');
--select statement
select * from dbo.SAPID
Single quote is an escape character in SQL much like other languages.Single quotes are escaped by doubling them up, just as shown in above example.
SQL Fiddle Demo

How to add '~' sign to a returned nvarchar value?

I am trying to add a '~' sign between two columns in SQL Server:
SELECT CODE + '~' + NAME FROM TEST_TABLE
It should return 'CCC~NNN' but it displays a newline instead of '~'. How can I fix this?
Maybe theres a hidden new line char in one of the selected fields. Try this:
SELECT REPLACE(REPLACE(CODE + '~' + NAME, CHAR(13), ''), CHAR(10), '') FROM TEST_TABLE
I would suggest using different column names in the future.
For now, it might suffice to simply surround your column names with brackets:
SELECT [CODE] + '~' + [NAME] FROM TEST_TABLE
Works fine for me.

struggling with creating Insert query

I create Insert statement for organization table like this:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)values('''+Nameofthecompany+''+','+Nameofthepersonresponsibleforrecruitment+','+PhoneNumber+','+MobileNumber+''')' from Organization
When I execute this statement I get insert statement. But the issue is where the value is null, it shows all columns null.
Example: (in database)
Name: xxxx
ContactPerson: zzzz
ContactNumber:444444
MobileNumber: null
so my insert statement looks like:
Null.
I want only that column provide null. other details showing properly. Is there any way in sql server? Help me anyone...
The result of concatenating anything to NULL, even itself, is always NULL. Workaround with ISNULL function:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)
values('''+ISNULL(Nameofthecompany, 'NULL')+''+','
+ISNULL(Nameofthepersonresponsibleforrecruitment, 'NULL')+','
+ISNULL(PhoneNumber, 'NULL')+','
+ISNULL(MobileNumber, 'NULL')+''')'
from Organization
Demo on SQLFiddle
Sure - just use ISNULL(..) to turn a NULL into e.g. an empty string:
SELECT
'INSERT INTO Organizations(Name, ContactPerson, ContactNumber, Mobilenumber) VALUES(''' +
ISNULL(Nameofthecompany, '') + '' + ',' +
ISNULL(Nameofthepersonresponsibleforrecruitment, '') + ',' +
ISNULL(PhoneNumber, '') + ',' + ISNULL(MobileNumber,'') + ''')'
FROM Organization
When you are adding each of the parameters to the SQL statement, you need to check whether they're null, and if so use the keyword NULL, otherwise include a literal string surrounded with single quotes, but bearing in mind that if the string contains any single quotes, they need to be replaced with two single quotes.
Update the SQL for each parameter something like the following:
CASE WHEN MobileNumber IS NULL THEN 'NULL' ELSE '''' + REPLACE(MobileNumber, '''', '''''') + '''' END

How to replace CHAR(13) in DB varchar(6000)?

Using ColdFusion and Microsoft SQL we are exporting data to an Excel Spreadsheet using the cfx_excel plugin. The data contains a varchar(6000) which has CHAR(13)/line-breaks inputted in each entry.
The line-breaks are appearing as square brackets every time the report is generated in Excel format.
How would I go about removing the CHAR(13) within a SQL query?
Thank you.
try this
update YourTable
set YourColumn =replace(YourColumn,CHAR(13),'')
or just for a select
SELECT replace(YourColumn,CHAR(13),'')
FROM YourTable
for char(10) and char(13) you can do this
SELECT replace(replace(YourColumn,CHAR(13),''),CHAR(10),'')
FROM YourTable
'' will replace it with a blank, if you want a space then use ' ' instead of ''
To replace both char(10) and char(13) you should be able to just do a
replaceList(textToReplaceIn,"#chr(10)#,#chr(13)#",",")
If that doesn't work you can just do 2 replaces as in
replace(replace(textToReplaceIn,chr(10),"","all"),chr(13),"","all")

Use a SELECT to Print a Bunch of INSERT INTOs

I have a bunch of records I want to move to another database and I just want to create a bunch of inserts that I can copy and paste. I've seen someone do this before but I can't figure it out. I'm not getting the escapes right.
It's something like this where 'Code', 'Description' and 'Absent' are the columns I want from the table.
SELECT 'INSERT INTO AttendanceCodes
(Code, Description, Absent)
VALUES
(' + Code + ',' + Description + ',' + Absent')'
FROM AttendanceCodes
The end result should be a slew of INSERTS with the correct values like this:
INSERT INTO AttendanceCodes
(Code, Description, Absent)
VALUES
('A','Unverified Absence','UA')
Check this
SELECT 'INSERT INTO AttendanceCodes (Code, Description, Absent) VALUES (''' + Code + ''',''' + Description + ''',''' + Absent''')'
FROM AttendanceCodes
try something like this:
SELECT
'INSERT INTO AttendanceCodes (Code, Description, Absent) VALUES ('
+ ISNULL(''''+CONVERT(varchar(8000),Code)+'''','null')
+ ','
+ ISNULL(''''+CONVERT(varchar(8000),Description)+'''','null')
+ ','
+ ISNULL(''''+CONVERT(varchar(8000),Absent)+'''','null')
+')'
FROM AttendanceCodes
This will handle NULLs. I'm not sure of your columns data types, but you can only concatenate strings, so I forced everything to varchar(8000), you can modify this as necessary.
For a generic stored procedure that takes a table name (and some other options) and generates insert statements for current data in that table, check out this handy
link.
It creates a procedure called sp_generate_inserts. You pass this procedure the table name, and it will generate all the insert statements to recreate the data.