Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Varchar to Bit


Convert Varchar to Bit

Author
Message
yan_aung
yan_aung
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 27

I have a varchar field with true and false contains in them..

Doing this:

Declare @MyValue as bit
set @MyValue = convert(bit, 'True')

Give me the following error message:

Server: Msg 245, Level 16, State 1, Line 2
Syntax error converting the varchar value 'True' to a column of data type bit.

I can do the case statement with when on it like this:
select Deleted = CASE Deleted WHEN 'False' THEN '1' Else '0' END

I want to know why convert function does not work?





JeffB
JeffB
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 74

In SQL, bits are represented by 0 or 1. It doesn't translate the string 'TRUE' to a 1. If the source data is a string 'TRUE', then a case statement has to be used.


Chris Hedgate
Chris Hedgate
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1499 Visits: 7
Bits are always 0 and 1. SQL Server does not have a boolean datatype, instead we use bit to represent them. Naturally it is impossible to convert a string to a number, so the convert from varchar to bit fails.

--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Giles McArdell
Giles McArdell
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 1

Quick and dirty fix:

DECLARE @Word varchar(5)
DECLARE @MyValue as bit
SET @Word = 'True'
select @MyValue = CASE @Word WHEN 'true' THEN 1 ELSE 0 END




"Don`t try to engage my enthusiasm, I don`t have one."

(Marvin)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search