Friday, February 10, 2012

change nchar to char

Can I change a double byte (nchar) to 2 single byte (char)?Win
You can lose date
CREATE TABLE #T
(
col NCHAR(2) NOT NULL
)
INSERT INTO #T VALUES (N'K?')
GO
SELECT * FROM #T
--K?
GO
ALTER TABLE #T ALTER COLUMN col CHAR(2)
GO
SELECT * FROM #T
--K?
"Win" <aaa@.aaa.com> wrote in message
news:uZPIZ8CYFHA.3320@.TK2MSFTNGP12.phx.gbl...
> Can I change a double byte (nchar) to 2 single byte (char)?
>
>
>|||Hi
nchar will explicitly convert to char see
http://msdn.microsoft.com/library/d...br />
2f3o.asp
If you want to separate each byte of an nchar into separate characters does
not make sense!
If you want to change the datatype for a column you can use alter table
statement, but (as in the recent conversation http://tinyurl.com/9bz6c) you
have to be careful with ANSI_PADDING and you may be better to create a
separate column, update it, and the drop the original column.
This may help!
SET ANSI_PADDING OFF
CREATE TABLE MyTable ( ncharcolumn nchar(10) NULL )
INSERT INTO MyTable ( ncharcolumn )
SELECT 'ABC '
UNION ALL SELECT 'DEF '
UNION ALL SELECT 'GHI'
SELECT ncharcolumn, DATALENGTH(ncharcolumn)
FROM MyTable
/*
ncharcolumn
-- --
ABC 20
DEF 20
GHI 20
*/
ALTER TABLE MyTable ADD charcolumn char(10) NULL
UPDATE MyTable
SET charcolumn = ncharcolumn
SELECT ncharcolumn, DATALENGTH(ncharcolumn), charcolumn,
DATALENGTH(charcolumn)
FROM MyTable
/*
ncharcolumn charcolumn
-- -- -- --
ABC 20 ABC 3
DEF 20 DEF 3
GHI 20 GHI 3
*/
ALTER TABLE MyTable ALTER COLUMN ncharcolumn char(10) NULL
SELECT ncharcolumn, DATALENGTH(ncharcolumn), charcolumn,
DATALENGTH(charcolumn)
FROM MyTable
/*
ncharcolumn charcolumn
-- -- -- --
ABC 10 ABC 3
DEF 10 DEF 3
GHI 10 GHI 3
*/
SET ANSI_PADDING ON
DROP TABLE MyTable
CREATE TABLE MyTable ( ncharcolumn nchar(10) NULL )
INSERT INTO MyTable ( ncharcolumn )
SELECT 'ABC '
UNION ALL SELECT 'DEF '
UNION ALL SELECT 'GHI'
SELECT ncharcolumn, DATALENGTH(ncharcolumn)
FROM MyTable
/*
ncharcolumn
-- --
ABC 20
DEF 20
GHI 20
*/
ALTER TABLE MyTable ADD charcolumn char(10) NULL
UPDATE MyTable
SET charcolumn = ncharcolumn
SELECT ncharcolumn, DATALENGTH(ncharcolumn), charcolumn,
DATALENGTH(charcolumn)
FROM MyTable
/*
ncharcolumn charcolumn
-- -- -- --
ABC 20 ABC 10
DEF 20 DEF 10
GHI 20 GHI 10
*/
ALTER TABLE MyTable ALTER COLUMN ncharcolumn char(10) NULL
SELECT ncharcolumn, DATALENGTH(ncharcolumn), charcolumn,
DATALENGTH(charcolumn)
FROM MyTable
/*
ncharcolumn charcolumn
-- -- -- --
ABC 10 ABC 10
DEF 10 DEF 10
GHI 10 GHI 10
*/
John
"Win" wrote:

> Can I change a double byte (nchar) to 2 single byte (char)?
>
>
>
>|||Can I convert 1 double byte character to 2 signle byte characters.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:ecvfkHDYFHA.2740@.TK2MSFTNGP14.phx.gbl...
> Win
> You can lose date
> CREATE TABLE #T
> (
> col NCHAR(2) NOT NULL
> )
> INSERT INTO #T VALUES (N'K?')
> GO
> SELECT * FROM #T
> --K?
> GO
> ALTER TABLE #T ALTER COLUMN col CHAR(2)
> GO
> SELECT * FROM #T
> --K?
>
>
> "Win" <aaa@.aaa.com> wrote in message
> news:uZPIZ8CYFHA.3320@.TK2MSFTNGP12.phx.gbl...
>|||Hi
Yes, but as Uri says there may be data loss or if you convert the two
bytes to ascii values there are not meaningful This may help explain:
DECLARE @.nstring NCHAR(1)
DECLARE @.charmask CHAR(2)
DECLARE @.binarymask BINARY(2)
SET @.nstring = NCHAR(260)
SELECT UNICODE(@.nstring),
NCHAR(UNICODE(@.nstring)),
CAST(@.nstring AS CHAR(1)),
ASCII(CAST(@.nstring AS CHAR(1)))
SELECT @.charmask = CAST(@.nstring as char(2)), @.binarymask =
CAST(@.nstring as binary(2))
SELECT @.charmask, ASCII(SUBSTRING(@.charmask, 1, 1)),
ASCII(SUBSTRING(@.charmask, 2, 1)),
@.binarymask, ASCII(SUBSTRING(@.binarymask, 1, 1)),
ASCII(SUBSTRING(@.binarymask, 2, 1))
John|||Hi John,
Actually I am using powerbuilder to make reports.
When I input chinese word (double byte) to a field, it will change it to 2
single byte (using powerbuilder).
But the datawindow can read the "2 single byte" data and print the chinese
word.
However, when I use web programs (ASP) and (nvarchar) to store the chinese
word, the report cannot read the double byte character.
It just shows ?.
Should I change the 1 double byte data to 2 single byte data?
How can I print the report in chinese word and using nvarchar to store data?
Win
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:1116937596.821455.153100@.g49g2000cwa.googlegroups.com...
> Hi
> Yes, but as Uri says there may be data loss or if you convert the two
> bytes to ascii values there are not meaningful This may help explain:
> DECLARE @.nstring NCHAR(1)
> DECLARE @.charmask CHAR(2)
> DECLARE @.binarymask BINARY(2)
> SET @.nstring = NCHAR(260)
> SELECT UNICODE(@.nstring),
> NCHAR(UNICODE(@.nstring)),
> CAST(@.nstring AS CHAR(1)),
> ASCII(CAST(@.nstring AS CHAR(1)))
> SELECT @.charmask = CAST(@.nstring as char(2)), @.binarymask =
> CAST(@.nstring as binary(2))
> SELECT @.charmask, ASCII(SUBSTRING(@.charmask, 1, 1)),
> ASCII(SUBSTRING(@.charmask, 2, 1)),
> @.binarymask, ASCII(SUBSTRING(@.binarymask, 1, 1)),
> ASCII(SUBSTRING(@.binarymask, 2, 1))
> John
>|||Hi
I don't know what powerbuilder is doing under the covers to make sense of
this. If your database is currently nchar then how do you know Powerbuilder
is using 2 single byte characters?
When your asp page displays a '?' this usually means your browser can not
interpret the character to display it. You may need to set the codepage as i
n
http://msdn.microsoft.com/library/d...ge
n.asp
This page and the links may also be worth reading:
http://support.microsoft.com/defaul...kb;en-us;303612
HTH
John
"Win" wrote:

> Hi John,
> Actually I am using powerbuilder to make reports.
> When I input chinese word (double byte) to a field, it will change it to 2
> single byte (using powerbuilder).
> But the datawindow can read the "2 single byte" data and print the chinese
> word.
> However, when I use web programs (ASP) and (nvarchar) to store the chinese
> word, the report cannot read the double byte character.
> It just shows ?.
> Should I change the 1 double byte data to 2 single byte data?
> How can I print the report in chinese word and using nvarchar to store dat
a?
> Win
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message
> news:1116937596.821455.153100@.g49g2000cwa.googlegroups.com...
>
>

No comments:

Post a Comment