Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
57 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 40

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 35

Accepted Solution

by:
Pawan Kumar earned 2000 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 35

Expert Comment

by:Pawan Kumar
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 27

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

885 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