solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page








Insert Extended Characters into a SQL Server Table Using OSQL and TSQL

By: | Read Comments (1) | Print

Jugal has 8+ years of extensive SQL Server experience and has worked on SQL Server 2000, 2005, 2008 and 2008 R2.

Related Tips: More

Problem
The OSQL utility uses ODBC to communicate with SQL Server. The user’s problem is that the ODBC driver he is using to connect to the database is performing translations on the character data in the T-SQL script. Extended characters, which are not in the standard ASCII character set, are translated by the driver based on drive settings. The character translation option is ON by default when SQL Server executes scripts through the OSQL utility.

Solution
We can resolve this issue by configuring DNS or T-SQL . To resolve this issue we will take the below example.


Steps to reproduce the issue

Step 1 Create an Example table by using the below code.
create table Example
(
 id int identity(1,1),
 extendedCharactervalue varchar(1000)
);
insert into Example(extendedCharactervalue) values('Rrotégé Company, LLC')

Step 2 Save below INSERT query into text file/SQL file. I have named the file Query.txt

insert into Example(extendedCharactervalue) values('Rrotégé Company, LLC')

Step 3 Execute the below OSQL statement from command prompt

Step 4 Now query the Example table and you can see the character é is converted to T


We can fix the below by using one of these solutions.

Solution - I Insert Extended character using DNS with OSQL

Step 1 Create ODBC System DSN. Go to Control Panel -- Administrative Tool -- SELECT Data Sources (ODBC)

Step 1.2 Select System DSN tab from Data Sources (ODBC) and click on Add button on the right of the screen.

Step 1.3 Select SQL Server from driver list and click on Finish button

Step 1.4 Enter the DSN Name and select Server name from list

Step 1.5 Choose the authentication mode and click on Next button

Step 1.6 Select the database name from list and click on Next button

 

Step 1.7 Uncheck the below checked button (Perform translation for character data) and click on Finish button

Step 1.8 In next screen you can see the test data source window, using that you can test the connectivity

Step 2 Save As script file as Unicode file (Open the script file in Text Editor (NOTEPAD))

Step 3 Execute the SQL script using OSQL by passing DSN Name and Script file as parameter

Now query the Example table you can see the below output.


Solution - II Using T-SQL

-- ascii function retuns the ascii value of extended character
select ascii('é')
-- char function converts the ascii value to character
select char(233)
insert into Example(extendedCharactervalue) 
select 'Rrot' + char(233) + 'g' + char(233) + ' Company, LLC'

Next Steps

  • Check out the above solution by adding more extended characters
  • Check the solution one without using a DSN and only saving script in the unicode file format.
  • Search out more OSQL switches.


Related Tips: More | Become a paid author


Last Update: 3/2/2010

Share: Share 






Comments and Feedback:

Friday, April 09, 2010 - 6:01:54 AM - urcuterajendra Read The Tip

Hey Jugal,

I have also checked your this post, it is working absolutely fine.

Thanks

Raj



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Find and fix SQL Server problems before they happen - SQL diagnostic manager now with predictive analysis!

SQL Monitor, server monitoring so easy, your boss could do it. Try it online.

Need SQL Server help and not sure where to turn? Reach out to the Edgewood experts for a Health Check starting at $995.

Free Trial: Get Proactive Insight with Spotlight® for SQL Server Enterprise.

Join the over million SQL Server Professionals who get their issues resolved daily.

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com