Avatar of zimmer9
zimmer9
Flag for United States of America asked on

For SQL Server 2008, if I have a table with a field defined as either varchar(255) or nvarchar(MAX), can I input a comma in place of a value when using an insert statement if the field has no value?

For SQL Server 2008, if I have a table with a field defined as either varchar(255) or nvarchar(MAX), can I input a comma in place of a value when using an insert statement, if the field has no value? Allow Nulls is checked.
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Zberteoc

8/22/2022 - Mon
lcohan

Sure you can. the difference between VARCHAR and NVARCHAR SQL types is that Nvarchar allows  input/storage of UNICODE characters and Varchar doesnt.

http://www.sqlserver.info/database-design/varchar-vs-nvarchar/
zimmer9

ASKER
OK, let's say I have a table with 14 fields that are defined as varchar(255) followed by the 15th field defined as bit, followed by 5 fields that are defined as varhar(255) followed by the 21st field defined as bit. How would you write an INSERT statement to load 1 record into this table using commas and a bit value of 1 for the 2 fields defined as bit.

For ex:

Insert Into dbo.tbl_CSL_Branches Values
(,,,,,,,,,,,,,,1,,,,,,1);
zimmer9

ASKER
When I use:

Insert Into dbo.tbl_CSL_Branches Values
 (,,,,,,,,,,,,,,1,,,,,,1);

I get the error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ','.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

Varchar[(n)]
      
It is Non-Unicode Variable Length character data type.
Example: DECLARE @varchar AS VARCHAR(100) = 'Pawan'
SELECT @varchar      

NVarchar[(n)]

UNicode Variable Length character data type. It can store both non-Unicode and Unicode (i.e. Japanese, Korean, Chineese etc) characters.
DECLARE @Nvarchar AS NVARCHAR(100)= 'Pawan'
SELECT @Nvarchar

Complete code--

CREATE TABLE testComma
(
	Id TINYINT NOT NULL
	Val1 VARCHAR(255) NOT NULL
	Val2 NVARCHAR(MAX) NOT NULL
)
GO


INSERT INTO dbo.tbl_CSL_Branches
VALUES (1,',',',')

GO
--

Open in new window


--
Pls try and let us know in case of any issues..
Zberteoc

Why not use this:
INSERT INTO YourTable (bit_col1, bit_col2) VALUES(1,1)

Open in new window