• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

HOw to Set 'bit' variable to True Or False IN SQL Server SPROC/t-SQL

How do I initialize a field defined a 'bit' to 'True' or 'False'

I want to initialize a 'bit' field in an output table to True.

I tried the literal 'True'
I am now trying to set a bit variable to true and load that into the table field but that isn't working either.

I want to initialize a few fields way out to the right to be either true or false.  RIght now it is unsuccessfully using the variables @Truevar and @FalseVar.

HOw can initialize these fields on the output table to be true, or in some case false.

Perhaps it's really simple, like true = 1 and false = 0.  Just want to make sure.

Here's my code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- 
-- =============================================
ALTER PROCEDURE [dbo].[aConvertspProdNA_wInSPORC]
   
AS
BEGIN
    SET NOCOUNT ON;

	declare     @WkDate DATE
	declare     @wkUser  nvarChar(10)
	declare     @TrueVar    bit
	declare     @FalseVar   bit

	set @wkUser='Convert'
	set @wkdate = GETDATE()
--	set @TrueVar = True
--	'set @Falsevar  = False  
--
-- Clear the COmment Table
--
  Execute dbo.spDeleteAllRecsFromSQLTable 'tblProperty_Addresses'
--
--  Now load it
--
    INSERT INTO dbo.tblProperty_Addresses (  PropertyRecID,		Muni,		LotBlock,	TieBreaker,		JTSAddr1,		JTSAddr2,		JTSAddr3,		JTSAddr4,		JTSSeqNumber,		Name1,			Name2,			Address1,		City,		State,		ZipCode,   JTSOldControlNum,	BillingAddressYN,	  IncludeOnLegal ,   SequenceOnLegal,  UnMailable,		AddressCheckNeeded,		MailingAddress_YN ,  InterestTypeID ,   DefaultAddress_YN,  Active_YN ,   DateAdded,	UserAdded )
        SELECT								W.PropertyRecID, W.MuniCode,	W.LotBlock,	W.TieBreaker,	W.AddrLine1,	W.AddrLine2,	W.AddrLine3,	W.AddrLine4,	W.SequenceNumber,	W.AddrLine1,	W.AddrLine2,	W.AddrLine3,	W.City,		W.State,	W.Zip,		W.ControlNumber,	@Truevar,					@Falsevar,				0,				W.UnMailable,	W.AddressCheckNeeded,	@Truevar,				 4,					@Truevar,				@Truevar,			@wkDate,	@wkUser

        FROM dbo.bView_buildProd_NA W

END

Open in new window

0
mlcktmguy
Asked:
mlcktmguy
2 Solutions
 
Ryan ChongCommented:
bit variable only can store 0 or 1, it cannot store neither 'True' nor 'False'

try declare like this:

declare     @yourVar    bit

Open in new window


in your Insert statement, use:

case when @yourVar = 1 then 'True' else 'False' end

Open in new window


if you're refer to a table's field directly, use:

case when yourTable.yourField = 1 then 'True' else 'False' end

Open in new window

0
 
ste5anSenior DeveloperCommented:
And when you use SQL Server 2012+, you can use the IIF() function instead of the CASE..WHEN..END expression:

IIF(yourTable.yourField = 1, 'True', 'False')

Open in new window

0
 
mlcktmguyAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now