Replace Zeros And Nulls With 1 In Table -- Using Case, But Not Working
Jul 20, 2005
Hi folks,
I'm doing calculations based on data in a table, but the data has some
zeros in the field I'm dividing by. I'm trying to write a script to
replace any field with 0 or null with 1, but it's not working. HEre's
what I've got:
Update A Set A.deptcode = A.deptcode,
A.type = A.Type,
A.Volume = (case A.Volume
When Null Then 1
When 0 then 1
Else A.Volume
End)
From Data_Unsorted A Join Data_Unsorted B On
A.deptcode = B.deptcode and A.type = B.Type
My table is data_unsorted and deptcode and type are my primary keys
Volume is the item I want to put 1 if null or zero, and I'd thing the
above statement would work, but it doesn't. This table has 383 rows,
and it says it updates 383 rows, but when I run the following query to
test:
select a.deptcode, a.type, a.volume
from data_unsorted a
where a.AveMonthVolume = 0 or a.AveMonthVOlume is null
It didn't work... still TONS of nulls and zero's. Is there a trick to
this???
In my database I have "null" for some values of the data and now I want to change that to zero '0' due to some requirement. So, Is there any function for that?? So that all the "null"s in the database are changed to '0's......
Hello, I have a simple question. Is it at all possible to replace columns which has nulls with blank spaces for a float data type column. The columns has null values( written)) in it in some rows and has numbers in other rows . I want to remove nulls before copying it to another file. Thanks
I have a field that may be null which is valid, and I am findingsomething I didnt expect when working with nulls.SELECT NULL/4.0 will return NULL (which I expect), however, when I testit with a case it does not:SELECT NULL/4.0 AS 'TEST1', TEST2 = CASE NULL/4.0 WHEN NULL THEN'HURAY' ELSE 'OH DARN' ENDI can work around by testing for NULL first else CASE ..., but I'd liketo understand why the CASE does not test for null.TIArobSQL 2005 Enterprise x64, SP1
For Example:SELECT Column1, Column2 FROM Table WHERE Column1 = ( SELECT CASE @Test4Nulls WHEN 1 THEN NULL ELSE Column1 END) Basically, I want to test for Nulls in a column if my variable @Test4Nulls is True, however, I never get any results back. I believe it is because I am Criteria = NULL instead of Criteria IS NULL, however, I cannot use IS in a case statement How do I test for nulls in a case statement
I have an instead of trigger for update on particular table which adds3 or so columns and puts the total in a 4th No matter what the input is(as long as there is one NULL) the total is always NULL. More thanlikely is that only one of the fields has a value in it and the restnull but i don't want to do the MAX in case that is not true.a quick visual aid from input and desired outputInput DatacolA colB ColC tot5 NULL NULL NULLCurrent OutputcolA colB ColC tot5 NULL NULL NULLDesired OutputcolA colB ColC tot5 NULL NULL 5How do i Manipulate the TSQL command to work correctly?Update tbl_lossChecksSETclc_totalFees = Inserted.mny_LegalFees + Inserted.mny_AdjusterFees +Inserted.mny_Expense ,
I have what I thought would be a simple problem: my SQL call has a few NULLs that I want to replace with 0s. Wherever I wanted this to happen, I put in code like this: [code]CASE colName WHEN NULL THEN 0 ELSE colName END as colName[/code] And it doesn't work...the column is the same as it was before I put in the case statement.
I'm trying to use a DTS package to import data from an excel file. A few tables keep throwing errors about not being able to insert null values.
Is there any way to just skip a row when a column is null? I know there are certain columns that will never be null, so if they are null, I know that I just have a messed up row in Excel.
I could probably do it by implying an SQL query and having it ignore null rows, but I can't find anything useful on SQL syntax where Excel files are concerned.
I have a query with huge number of case statements. Basically I need to short this query with getting rid of these hundreds of CASE statements.
Because of the nature of the application I am not allowed to use a function, and just wondering if there is a possible way to rewrite this with COALESCE().
SELECT CASE WHEN A.[COL_1] LIKE '%cricket%' THEN 'ck' + ',' ELSE '' END + CASE WHEN A.[COL_1] LIKE '%soccer%' THEN 'sc' + ',' ELSE '' END + .... CASE WHEN A.[RESIUTIL_DESC] LIKE '%base%ball' THEN 'BB' + ',' ELSE '' END FROM TableName A
I am looking for some syntax to help me with a traditional search/replace style requirement. I am wanting to examine the contents of one column and populate another.
similar to this SQL case statement
CASE ProductLine WHEN 'R1' THEN 'Road' WHEN 'M1' THEN 'Mountain' WHEN 'T1' THEN 'Touring' WHEN 'S2' THEN 'Other sale items' ELSE 'Not for sale' END,
the twist is that R1, M1 etc. can appear anywhere in the ProductLine column.
I have a situation where I want to update a column if and only if it is null.
UPDATE Employee SET VEmployeeID = CASE WHEN E.VEmployeeID IS NULL THEN ves.VEmployeeID END FROM Employee E INNER JOIN VEmployeeStaging VES ON E.EID= VES.EID
But what happens is when I run the procedure every other time I run it, it changes everything to null. The other times it puts the VEmployeeID in.
So what is happening is the times when it is not null (where it is not supposed to do anything) it puts a null in. The next time it works.
SELECT Gruppo_Assegnatario, [100] as stato1, [101] as stato2, [102] as stato3 FROM ( select
[Code] ...
That extracts only zeros (columns "stato1", "stato2", "stato3"):
Gruppo_Assegnatariostato1stato2stato3 SDB_BE Vita Antiriciclaggio0 00 SDB_BE Vita Assistenza clienti000 SDB_BE Vita Emissione000 SDB_BE Vita Gestione Rendite000 SDB_BE Vita Liquidazioni000
[Code] ....
Unlike the "SourceTable":
select CASE_ID_, Stato, Gruppo_Assegnatario FROM TicketInevasiPerGruppoEStato extracts
CASE_ID_ Stato Gruppo_Assegnatario HD0000003736734 AssegnatoSDB_GBS Variazione HD0000003736739 AssegnatoSDB_GBS Variazione HD0000003736743 AssegnatoSDB_GBS Variazione HD0000003736783 AssegnatoSDB_GBS Variazione HD0000003736806 SospesoSDB_BE Vita Selezione
[Code] ....
How can I get the right count in the first data mining replacing the zeros (columns "stato1", "stato2", "stato3")?
I am preparing SSIS package programmetically using C#, in a web wethod, to perform fuzzy lookup and other transformations in to sql server data.
now situaltion is like this,
there are three system (independent m/cs)
m/c 1 :- webserver
m/c 2 :- sql database (sqlserver)
m/c 3 :- client (it can be any where , we are using onc click deplyment)
Now case is like this.
A user sitting at m/c no. 3 (a thin client) has one-click-deployed application, clicks the button "Create SSIS Package" , which sends this message to webserver (i.e. m/c 2 ) where C# code constructs the package programmatically,
there are three component inside package
1. oldedb Source (a sql server table -- sqlserver is at m/c no 3)
2. fuzzylookup component
3. oledb destination (a sql server table -- sqlserver is at m/c no 3)
Now 1 and 3 component of the package need to connect to sqlserver for initialization there metadata from actual tables.
// Tries to connect to sqlserver hosted at different m/c
srcDesignTime.AcquireConnections(null);
srcDesignTime.ReinitializeMetaData();
srcDesignTime.ReleaseConnections();
now the 3rd last line which says srcDesignTime.AcquireConnections(null);
tries to open the connection to the selserver (in order to get information abt actual table and its columns) which is installed at another m/c, and this is possible cause of error (i think !!)
there are two cases in one case i am getting an error and in second case it is working just fine..
case 1. If i use (local webservice) web service to create package it works.
case 2 if i use published (on local IIS ) webservice to create package, It fails (at the code srcDesignTime.AcquireConnections(null); )
so i think problem is related to some authorization, may be i am wrong !!1
Please help me.
thanks in advance
Pradeep
(I am really looking for a reply for MR. Jamie Thomson )
When i do a select on my emplee table for rows with null idCompany i dont get any records
I then try to modify the table to not allow a null idCompany and i get this error message:
'Employee (aMgmt)' table - Unable to modify table. Cannot insert the value NULL into column 'idCompany', table 'D2.aMgmt.Tmp_Employee'; column does not allow nulls. INSERT fails. The statement has been terminated.
To make the long story short, we have a situation where there can be different datatypes for a column. For example, 'AttributeValue' can be a datetime, int, float etc. My DBA is suggesting the following design,
For any AttributeId, only one of the value columns will have a value and the rest will be NULLs. I feel that this is a bad design. (FYI, We decided against SQLVariant for specific reasons). I have suggested, we create a seperate table for each datatype, like
tblAttribute: AttributeId, AttributeDataTypeId
tblAttributeValueFloat: AttributeId INT, AttributeValue FLOAT tblAttibuteValueDateTime: AttributeId INT, AttributeValue DATETIME etc for each datatype.
Depending on the AttributeDataTypeId in the Attribute table go to the specific table and pick up the value. This way, we don't need to have too many NULLs in a row. Which of the 2 do you think is a better design? And if you agree with me, what would be the points to support it.
I'm trying to insert any null values into a temp table. I'm storing the ixo_rlt_code as OldRole, and 'Other' as NewRole. I need both the ixo_rlt_code as OldRole and 'Other' as NewRole in the temp talbe in order to run an update I'm writing.
I know a case statement isn't necessary here, but this section is only one small part of a larger script, so I just used it to be consistent, and it will be easier to make any future changes this way. Below is my code and the error message I'm getting...
select ixo_key, ixo_title as Title, ixo_rlt_code as OldRole, CASEWHEN ixo_title is null or ixo_title = '' or ixo_title = ' ' or ixo_title = ' ' THEN 'Other'
END as NewRole
from co_customer (nolock) join co_individual_x_organization (nolock) on ixo_key = cst_ixo_key --and ixo_delete_flag = 0 where cst_delete_flag = 0 and cst_type = 'Individual' and ixo_rlt_code is null
This is the Error Message I get: Server: Msg 515, Level 16, State 2, Line 1095 Cannot insert the value NULL into column 'NewRole', table 'tempdb.dbo.#temp_______________________________________________________________________________________________________________00000001698C'; column does not allow nulls. INSERT fails. The statement has been terminated.
I apologize if this has been posted/asked before... a search of the Forum for keyword "NULL" doesn't return any result (not even a 0 found).
When I import an XLS file into SQL2000, everything goes fine, except that every column after my data has <NULL> in it. How do I prevent this from happening, or fix it?
Our database stores vehicle data in one table, but 3 different types of data are stored in the one table. The table contains all the columns for all 3 types so when you query the table you get at least 3 rows back with null values for all the columns that don't apply to that record. The data is imported to the table when it's updates so there's a possibility that they're updated at different times so they have a different BATCH like:
BATCH TYPE ID RATING INSURANCE SAFETY 300 SAFE 123 NULL NULL A 300 INS 123 NULL YES NULL 250 RATE 123 A NULL NULL
What I'd like returned is: ID, RATING, INSURANCE, SAFETY 123 A YES A
I'm trying to do a case statement to pull the data down, but I keep ending up with multiple rows because of all the nulls. I tried doing a SUM of the case statement with an ISNULL(SAFETY,0) but I can't SUM char values. I can probably do this with 3 temp tables to load the data that I want for each TYPE into them and then select and join them together, but is there a better way to do this?
I'm inserting data from a c# webservice into a table via a stored procedure, but I get a Column does not allow nulls on the @alert_id column/field. It is set as int and allow nulls is not ticked.
Here's the sql:
USE [aren] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [aren1002].[ArenAlertInsert]
Hi,I basically have two tables with the same structure. One is an archiveof the other (backup). I want to essentially insert the data in to theother.I use:INSERT INTO table ( column, column .... )SELECT * FROM table2Now, table2 has a rule on various columns:@CHARACTER IN ('Y','N')but the column allows nulls, in the design view is says so anyway.When I run this query I get:A column insert or update conflicts with a rule imposed by a previousCREATE RULE statement. The statement was terminated. The conflictoccurred in database 'database', table 'table', column 'column'.The statement has been terminated.Obviously, I've changed the names of everything.The only data in those columns which could possibly conflict with therule is the NULL value. Any ideas why this doesn't work?Thanks.
is there an elegant way to use one equals sign in a where clause that returns true when both arguments are null, and returns true when neither is null but both are equal and returns false when only one is null?
I am trying to import an Excel Spreadsheet into SQL2005. There is a column in the spreadsheet that has character values, and numbers. I have formatted the numbers as text on the spreadsheet. I have declared the column on the table as char/varchar/nchar, but whatever I do, the numbers don't get imported into the table, but show up as nulls. Any idea why?
I have two SSIS packages that import from the same flat file into the same SQL 2005 table. I have one flat file connection (to a comma delimited file) and one OLE DB connection (to a SQL 2005 Database). Both packages use these same two Connection Managers. The SQL table allows NULL values for all fields. The flat file has "empty values" (i.e., ,"", ) for certain columns.
The first package uses the Data Flow Task with the "Keep nulls" property of the OLE DB Destination Editor unchecked. The columns in the source and destination are identically named thus the mapping is automatically assigned and is mapped based on ordinal position (which is equivalent to the mapping using Bulk Insert). When this task is executed no null values are inserted into the SQL table for the "empty values" from the flat file. Empty string values are inserted instead of NULL.
The second package uses the Bulk Insert Task with the "KeepNulls" property for the task (shown in the Properties pane when the task in selected in the Control Flow window) set to "False". When the task is executed NULL values are inserted into the SQL table for the "empty values" from the flat file.
So using the Data Flow Task " " (i.e., blank) is inserted. Using the Bulk Insert Task NULL is inserted (i.e., nothing is inserted, the field is skipped, the value for the record is omitted).
I want to have the exact same behavior on my data in the Bulk Insert Task as I do with the Data Flow Task.
Using the Bulk Insert Task, what must I do to have the Empty String values inserted into the SQL table where there is an "empty value" in the flat file? Why & how does this occur automatically in the Data Flow Task?
From a SQL Profile Trace comparison of the two methods I do not see where the syntax of the insert command nor the statements for the preceeding captured steps has dictated this change in the behavior of the inserted "" value for the recordset. Please help me understand what is going on here and how to accomplish this using the Bulk Insert Task.
I created a Fact Table with 3 Keys from dimension tables, like Customer Key, property key and territory key. Since I can ONLY have one Identity key on a table, what do I need to do to avoid populating NULLs on these columns..
Hi! I've some data in a table, and I want to add these records to another table, but I tried yet with select ... Into ... and I lose all the data from this one. I need to ADD the data from table to table. Any suggestion? Thx
acutally due to certain reason i have changed the few tables name in my database, but now i m facing problem in my software, i have alot of procedures and function as well in my database and these are using old table name, Is there any script that i can use to replace the name of the tables. i have checked few procedure/function in which i use table name with different style like [dbo].[tablename] and in some places i just use only [tablename].