Log in  ::  Register  ::  Not logged in

## Bit by bit

 Author Message bhakti-sonu SSC Veteran Group: General Forum Members Points: 200 Visits: 78 I try this out with @bit = 0 it gives message "no" if @bit = -500 ,it gives message "yes", if @bit = -1 then also it gives "yes".The Explanation Given for Answer : Bit constants are represented by the numbers 0 or 1, if a number larger than one is used, it is converted to one.but what about the values less than 1, still they consider as 1Please explain me more about the negative values used for bit datataype. alpeshgediya SSC Veteran Group: General Forum Members Points: 236 Visits: 60 for bit datatype, any value which in non-zero is converted to bit 1.try this you will get better understading, declare @bit bitset @bit = -0select @bitdeclare @bit bitset @bit = +0select @bitas mathematical there is no concept of +/- 0 ..is for only understading purpose. ChiragNS SSChampion Group: General Forum Members Points: 14775 Visits: 1865 why is'nt BIT treated like other datatypes? "Keep Trying" Marry Krissmess SSC-Enthusiastic Group: General Forum Members Points: 154 Visits: 29 Bob Cullen-434885 (1/28/2010)OK. I am in the 17% who got it wrong - I replied "Error". And now I know. But will somebody kindly explain why BIT is treated any differently to TINYINT, SMALLINT, INT, etc. It is, after all, only a type that has a range of valid values. If the others report an overflow error when I try to assign values outside their permitted range, why not BIT also?I am exactly feeling the same. I think a database should not be so forgiving. In programming languages this may be treated as boolean but here it should be a numeric range and any thing else than 0 or 1 should throw an error. Toreador SSCertifiable Group: General Forum Members Points: 5985 Visits: 8281 Marry Krissmess (2/5/2010)In programming languages this may be treated as boolean but here it should be a numeric range and any thing else than 0 or 1 should throw an error.Why? The Bit datatype is a boolean, so why is it so surprising that it is treated as such? Paul White SSC Guru Group: General Forum Members Points: 80574 Visits: 11400 Marry Krissmess (2/5/2010)I am exactly feeling the same. I think a database should not be so forgiving. In programming languages this may be treated as boolean but here it should be a numeric range and any thing else than 0 or 1 should throw an error.Try it with the strings 'true' and 'false' - that will really upset you! Paul WhiteSQLPerformance.comSQLblog.com@SQL_Kiwi Paul White SSC Guru Group: General Forum Members Points: 80574 Visits: 11400 ChiragNS (1/31/2010)why is'nt BIT treated like other datatypes?It is. The value supplied is implicitly converted to a BIT, according to the conversion rules. Paul WhiteSQLPerformance.comSQLblog.com@SQL_Kiwi shini2007 SSC Rookie Group: General Forum Members Points: 31 Visits: 38 I think":" Converting to bit promotes any nonzero value to 1" not greater than zero only Because DECLARE @bit BITSET @bit = -14IF @bit = 1PRINT 'yes'ELSE PRINT 'no'is also give yes. Paul White SSC Guru Group: General Forum Members Points: 80574 Visits: 11400 shini2007 (9/16/2010)I think":" Converting to bit promotes any nonzero value to 1" not greater than zero only Because DECLARE @bit BITSET @bit = -14IF @bit = 1PRINT 'yes'ELSE PRINT 'no'is also give yes.Non-zero does not mean greater than zero, it means not zero.-14 is not zero, so it is converted to 1, as I said.Does that make sense? Paul WhiteSQLPerformance.comSQLblog.com@SQL_Kiwi kapil_kk SSChampion Group: General Forum Members Points: 10707 Visits: 2778 Dave62 (1/28/2010)A little more information about the BIT type. The bit type has 3 states: NULL, 0, and 1.When first declared and unset a NULL will be returned.Only a 0 or the string 'False' will return a 0.Any positive or negative number or the string 'True' will return a 1.Any string other than 'True' or 'False' will return an error.The following script illustrates the points above:`Set NOCOUNT OnDeclare @blnFlag bitSelect @blnFlag As [Unset Returns]Set @blnFlag = 0Select @blnFlag As [Set to 0 Returns]Set @blnFlag = 144Select @blnFlag As [Set to 144 Returns]Set @blnFlag = -144Select @blnFlag As [Set to -144 Returns]Set @blnFlag = 'False'Select @blnFlag As [Set to 'False']Set @blnFlag = 'True'Select @blnFlag As [Set to 'True']Set @blnFlag = 'Dave'Select @blnFlag`The following resuts are returned:Unset Returns-------------NULLSet to 0 Returns----------------0Set to 144 Returns------------------1Set to -144 Returns-------------------1Set to 'False'--------------0Set to 'True'------------1Msg 245, Level 16, State 1, Line 22Conversion failed when converting the varchar value 'Dave' to data type bit.Dave+1 :-) _______________________________________________________________To get quick answer follow this link:http://www.sqlservercentral.com/articles/Best+Practices/61537/

## 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
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.