Solved

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?

Posted on 2016-09-20
6
32 Views
Last Modified: 2016-09-21
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.
0
Comment
Question by:zimmer9
6 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 41807641
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/
0
 

Author Comment

by:zimmer9
ID: 41807708
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);
0
 

Author Comment

by:zimmer9
ID: 41807838
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 ','.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41807847
Comma is field separator in the syntax thats why it is not working. Also we have supply comma in single quotes as it a string value

@Author -- Pls use below

--

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

--

Open in new window

0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41807850
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..
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41808710
Why not use this:
INSERT INTO YourTable (bit_col1, bit_col2) VALUES(1,1)

Open in new window

1

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now