I have a case statement I need to refer to several times in my select statement and it's quite long. I would like to just refer to if by name. I tried to create a variable for the Case statement (see below) but I get an error that says, "
Sub query returned more than 1 value. This is not permitted when the sub query follows =, !=, <, <= , >, >= or when the sub query is used as an expression."
USE GP05
GO
DECLARE @EmpID nvarchar (6)
SET @EmpID = (SELECT CASE WHEN NOT(dbo.BE010130.EMPID_I IS NULL) THEN dbo.BE010130.EMPID_I
WHEN dbo.BE010130.EMPID_I IS NULL AND NOT(dbo.UPR00500.EMPLOYID IS NULL) AND dbo.UPR00600.EMPLOYID IS NULL THEN dbo.UPR00500.EMPLOYID
WHEN dbo.BE010130.EMPID_I IS NULL AND dbo.UPR00500.EMPLOYID IS NULL AND NOT(dbo.UPR00600.EMPLOYID IS NULL) THEN dbo.UPR00600.EMPLOYID
WHEN dbo.BE010130.EMPID_I IS NULL AND NOT(dbo.UPR00500.EMPLOYID IS NULL) AND NOT(dbo.UPR00600.EMPLOYID IS NULL) THEN dbo.UPR00500.EMPLOYID
END FROM dbo.BE010130 FULL OUTER JOIN dbo.UPR00500 ON dbo.BE010130.EMPID_I = dbo.UPR00500.EMPLOYID AND dbo.BE010130.BENEFIT = dbo.UPR00500.DEDUCTON
FULL OUTER JOIN dbo.UPR00600 ON dbo.BE010130.BENEFIT = dbo.UPR00600.BENEFIT AND dbo.BE010130.EMPID_I = dbo.UPR00600.EMPLOYID)
SELECT @EmpID
I can get around this by simply retyping the case statement when I refer to it in the where clause, but I would prefer not to do this. Can someone point a newbie in the right direction? I have 5 case statements in this query and it starts to look pretty ugly when you have to retype them multiple times.
The SUBSTRING function returns @domain.com. I don't want any results to be displayed from my sql statement if it equals @myemail.com. I have other cases too that are not relevant to my question. I'm getting an error that says invalid syntax near <
SELECT DISTINCT CASE CUST_EMLAD_TX WHEN SUBSTRING(CUST_EMLAD_TX, CHARINDEX('@',CUST_EMLAD_TX), LEN(CUST_EMLAD_TX)) <> '@myemail.com' THEN CUST_EMLAD_TX END FROM ...
I am trying to have all these calulcation int one column under AccessFeeFinal, but it is not working. It was working when I just had the one case statement, but we needed to add another one in because we were getting some blank info in the column. Below are the two statement then I added a third on to show you how I think it should be, but it is not working for me. Please help me out if you can. I was thinking maybe I needed to use an IF Statement.
CASE clm_att1 WHEN 'NG' THEN (clm_H30)*(clio_fee04/100) WHEN 'NA' THEN '0.00' WHEN 'AF' THEN (clm_H30)*(clio_fee04/100)*.65 ELSE Null END as AccessFeeFinal,
CASE clm_att1 WHEN 'NG' THEN (clm_sppo)*(clio_fee04/100) WHEN 'NA' THEN '0.00' WHEN 'AF' THEN (clm_sppo)*(clio_fee04/100)*.65 ELSE Null END as AccessFeeFinal,
CASE clm_att1 WHEN 'NG' THEN (clm_H30)*(clio_fee04/100) WHEN 'NG' THEN (clm_sppo)*(clio_fee04/100) WHEN 'NA' THEN '0.00' WHEN 'AF' THEN (clm_H30)*(clio_fee04/100)*.65 WHEN 'AF' THEN (clm_sppo)*(clio_fee04/100)*.65 ELSE Null END as AccessFeeFinal,
I have a problem with using CASE in my database, Here is the code that I am using but not working
SELECT Computers.[Computer ID], Computers.Model, Computers.[Dell Service Tag], Users.[First Name] CASE WHEN Users.[first name] is NULL THEN Users.[user ID] ELSE Users.[First Name]+' '+Users.[Last Name] AS NAME ENDFROM Computers INNER JOIN Users ON Computers.[Computer ID]=Users.[Computer ID]ORDER BY Users.[First Name];
Could you please tell me where is wrong?Thanks in advance,Azi
I have this case statement below it is for an access fee. My problem that I am having is that now I am getting a number that is huge. Example, 8.2350000000. I need this to be rounded up and numbers srinked to 8.24, how can I add that in my case statement below? Please help if you can.
Thanks
CASE clm_att1 WHEN 'NG' THEN CASE clm_h30 WHEN 0 THEN cast(clm_sppo as decimal(12,2))*(cast(clio_fee04 as decimal (12,2))/100) ELSE cast(clm_H30 as decimal(12,2))*(cast(clio_fee04 as decimal (12,2))/100) END WHEN 'NA' THEN '0.00'
WHEN 'AF' THEN CASE clm_h30 WHEN 0 THEN cast(clm_sppo as decimal(12,2))*(cast(clio_fee04 as decimal (12,2))/100) ELSE cast(clm_H30 as decimal(12,2))*(cast(clio_fee04 as decimal (12,2))/100)*.65 END
I am trying to use a global variable in a case when and am not getting the correct results. If I use static data, it works fine.
Here is a tableless example, which should return Shipper. Any ideas are appreciated.
Code:
declare @shipperGBS varchar(3000) declare @sQuote char set @sQuote = char(39) set @shipperGBS = 'ACI,ADO,ALD,AMS,AWB' set @shipperGBS = Replace(@shipperGBS, ',', @sQuote + ',' + @sQuote) set @shipperGBS = @sQuote + @shipperGBS + @sQuote select case when ('ACI' IN (@shipperGBS)) then 'Shipper' else 'Consignee' end as ClientCharge
I want to be able to have a single select statment:
SELECT TOP 1 Call.JobNum, Call.CallID, Call.Company, Call.LastCallTime FROM ClientJob INNER JOIN Client ON ClientJob.ClientID = Client.ClientID INNER JOIN Call INNER JOIN Login ON Call.JobNum = Login.JobNum ON ClientJob.JobNum = Login.JobNum WHERE (Login.LoginID = 3) AND (Call.Status = 0) AND (DATEDIFF(hh, Call.LastCallTime, getdate()) > 10) ORDER BY Call.CallID
but with this select statment I also want to set a variable:
declare @variable int
SELECT TOP 1 Call.JobNum, @variable = Call.CallID, Call.Company, Call.LastCallTime FROM ClientJob INNER JOIN Client ON ClientJob.ClientID = Client.ClientID INNER JOIN Call INNER JOIN Login ON Call.JobNum = Login.JobNum ON ClientJob.JobNum = Login.JobNum WHERE (Login.LoginID = 3) AND (Call.Status = 0) AND (DATEDIFF(hh, Call.LastCallTime, getdate()) > 10) ORDER BY Call.CallID
Now SQL Server does not like this, can not set a variable in a multiple select statment. I NEED to do this all in one step if possible. Any suggestions?
My Problem is :im my transaction i use insert code like the following :
" Insert into TAB1 (F1,F2,F3) select a.F1,b.F2,b.F3 from TAB2 a,TAB3 b where a.KY1= b.KY1 and b.ORDN in (Select ORDN from OTAB where USER_ID = 'MIKE')"
In order to optimise my code , instead of using a subquery in my select (I have different insert in my transaction with the same subquery). I would like to DECLARE a varibale which will contain the select of the Subquery. and then use it im my different insert. some thing like this.
" BEGIN TRANSACTION DECLARE @OrdSelect int Set @OrdSelect = (Select ORDN from OTAB where USER_ID = 'MIKE') Insert into TAB1 (F1,F2,F3) select a.F1,b.F2,b.F3 from TAB2 a,TAB3 b where a.KY1= b.KY1 and b.ORDN in @OrdSelect COMMIT Tran "
I know that the @OrdSelect will receive the last value of the select not an array of values. which will make my transaction incorrect. I dont want to use Cursor to resolve this issue Too.
Using SQL Server 7 I am trying to modify an existing stored proc and make it more flexible. The below example represents the first part of that proc. The temp table that it should return is then used by another part of the proc (this query represents the foundation of my procedure). I need to figure a way to change the SQL Select statement, choosing between C.CONTRACTCODE and CB.EMPLOYERCODE on the fly. The query below will run but no records are returned. I am starting to believe/understand that I may not be able to use the @option variable the way I am currently.
I've tried creating two SQL statements, assigning them as strings to the @option variable, and using EXEC(@option). The only problem with this is that my temp table (#savingsdata1) goes out of scope as soon as the EXEC command is complete (which means I can not utilize the results for the rest of the procedure). Does anyone know how I can modify my procedure and incorporate the flexibility I've described?
Thanks,
Oliver
CREATE PROCEDURE test @ContractCode varchar(10), @dtFrom datetime, @dtTo datetime, @Umbrella int
AS
declare @option varchar(900)
if @umbrella = 0 set @option = 'c.contractcode' else set @option = 'cb.employercode'
select c.claimsno, c.attenddoctor, c.patientcode, p.sex, cb.employercode into #SavingsData1 from claimsa c inner join Patient p on c.patientcode = p.patientcode inner join claimsb cb on c.claimsno = cb.claimno where @option = @ContractCode and c.dateentered between @dtFrom and @dtTo and c.claimsno like 'P%' and p.sex in('M','F') and c.attenddoctor <> 'ZZZZ'
In my report, I am having Listbox for users to choose Country, City & Company. The user can choose Country. Based on the country selection, cities will be listed out. Based on the city selection, Companies will be listed out. They can choose companies.
Now, I have to show two set of results.
A. List of Companies as per selection ( dataset with equal to selection )
B. List of Companies which are not selected ( ie dataset with not equal to selection )
I have created a dataset with all companies and filter it by selection. When I tried with the filter option in the Dataset, I am able to check for only one value and not for multiple value. If the selection is one company, then I can filter it. But if they choose 5 companies, I am not not able to filter it. Is there any other option I can try out.
I have two tables I need to Select the record from the First table and insert them into the second table and delete the record from the first table how can i do that with the SQL Statment?
I'm producing a script to output a file containing data in a required format. Part of this script requires me to change a person id field to something else for a number of people.
I can do this no problem using a script like:
CASE WHEN person_id = '12345678' THEN '24681012'
However I have about 400 people to do and don't really want a 400 line script that will take me about a week to write....!!
Is there a way of doing this passing a variable??
I have created a table (TRANSFER) containing the old ID (OLD_ID) and a field containing the new id (NEW_ID), with all the appropriate data in.
How could I use this table with a variable to get the required results?
Select Case l.GLTransactionTypeID When 'Asset' --AND l.GLTransactionDate BETWEEN convert(datetime,'1/1/2007')AND convert(datetime,'12/31/2007') and l.GLTransactionSource=@assetID Then Begin Set @Cost=l.GLtransactionAmount end end from LedgerTransactions l,FixedAssets f where l.GLtransactionsource=f.assetID and l.GLtransactionsource= @assetID
I am trying to gather counts for table imports made for files from friday - sunday and create a variable that will be the body of an email. I'd like to use either a case statement or a while statement since the query is the same but the values must be collected for each day (friday, saturday and sunday) and will all be included in the same email.
I have declared all variables appropriately but I will leave that section of the code out.
Select @ifiledate = iFileDate from tblTelemark where iFileDate = CASE WHEN iFileDate = REPLACE(CONVERT(VARCHAR(10), GETDATE()-3, 101), '/','') THEN
Select @countfri1 = Count(*) from tbl1 Select @countfri2 = Count(*) from tbl2 Select @countfri3 = Count(*) from tbl3 Select @countfri4 = Count(*) from tbl4
WHEN iFileDate = REPLACE(CONVERT(VARCHAR(10), GETDATE()-2, 101), '/','') THEN Select @countsat1 = Count(*) from tbl1 Select @countsat2 = Count(*) from tbl2 Select @countsat3 = Count(*) from tbl3 Select @countsat4 = Count(*) from tbl4
WHEN iFileDate = REPLACE(CONVERT(VARCHAR(10), GETDATE()-1, 101), '/','') THEN Select @countsun1 = Count(*) from tbl1 Select @countsun2 = Count(*) from tbl2 Select @countsun3 = Count(*) from tbl3 Select @countsun4 = Count(*) from tbl4
I have experienced problem while trying to use variable with expression based on several other variables in tasks running parallel.
The details are as following:
There is a SSIS package with simple Control flow: one Script Task which actually do nothing and two Execute Process Tasks, they run after Script Task in parallel. Then there are three simple (EvaluateAsExpression = False) string variables ServerName, Folder and JobNumber with values ServerName = \test?, Folder = test? and JobNumber = 12345?. And there is one variable FullPath with expression @[User:: ServerName] + "\" + @[User::Folder] + "_" + @[User::JobNumber]. All the variables are of the Package scope. Then in Execute Process Tasks I have similar expressions based on FullPath variable: Execute Process Task 1 has expression @[User::FullPath] + "\date.bat" and Execute Process Task 2 has @[User::FullPath] + "\time.bat" one. As you understand these expressions define what exactly task should execute.
Then Im going to execute package from command line so appropriate XML configuration file has been created. The file contains following values for variables described above: ServerName = \LiveServer?, Folder = Job? and JobNumber = 33091?.
After series of consequent executions I have got following log file:
¦ Execute Process Task 1¦ Executing the process \LiveServerJob_33091date.bat?
¦ Execute Process Task 2¦ Executing the process \Test est_12345 ime.bat?
¦ Execute Process Task 1¦ Executing the process \Test est_12345date.bat?
¦ Execute Process Task 2¦ Executing the process \LiveServerJob_33091 ime.bat?
¦ Execute Process Task 1¦ Executing the process \LiveServerJob_33091date.bat?
¦ Execute Process Task 2¦ Executing the process \Test est_12345 ime.bat?
¦ Execute Process Task 1¦ Executing the process \LiveServerJob_33091date.bat?
¦ Execute Process Task 2¦ Executing the process \LiveServerJob_33091 ime.bat?
¦
As you can see one of Execute Process Tasks usually receive correct value of the expression (based on values of variables from the configuration file) while another - incorrect one (based on default? values of variables set directly in package). Sometimes wrong value appears in Task 1, next time in Task 2. Situations when both expressions in tasks evaluated correctly are very rare.
Then if you add some more Execute Process Tasks with similar expressions in the package (for ex. simply by copying existing tasks) youll get a good chance to catch error like this:
OnError,,,Execute Process Task 1,,,8/17/2007 2:07:12 PM,8/17/2007 2:07:12 PM,-1073450774,0x,Reading the variable "User::FullPath" failed with error code 0xC0047084.
OnError,,,Execute Process Task 1,,,8/17/2007 2:07:12 PM,8/17/2007 2:07:12 PM,-1073647613,0x,The expression "@[User::FullPath] + "\time.bat"" on property "Executable" cannot be evaluated. Modify the expression to be valid.
Seems variable with expression FullPath is locked during evaluation by one of the parallel tasks in such a way that another task cant read it value correctly. Can someone help me with the issue? Maybe there are some options I missed which could prevent such behavior of application? Please let me know how I can make the package work correctly.
Can someone tell me why these do not equal? The rec_day is 10 ? WHERE (CAST(DATEPART(Q, GETDATE()) * 3 - 2 AS VARCHAR(2)) + '/' + CAST(rec_day AS VARCHAR(2)) + '/' + CAST(YEAR(GETDATE()) AS VARCHAR(4))) = GETDATE()
hi does http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764584332.html = http://www.wrox.com/WileyCDA/WroxTitle/productCd-076457955X.html ?? and what is the difference ?? and which one of these you advice me to read for beginner ?? thank you
SELECT website FROM dbo.wce_contact WHERE (NOT (Mail1Date IS NULL)) AND (Mail2Date IS NULL) AND (Mail3Date IS NULL) AND (Mail4Date IS NULL) AND (Mail5Date IS NULL) AND (Mail6Date IS NULL) AND (Mail7Date IS NULL) AND (Mail8Date IS NULL) AND (Mail9Date IS NULL) AND (Mail10Date IS NULL) AND (Mail11Date IS NULL) AND (Mail12Date IS NULL) AND (Mail13Date IS NULL) AND (Mail14Date IS NULL) AND (Mail15Date IS NULL) AND (Mail16Date IS NULL) AND (IDStatus IS NULL OR IDStatus = '') AND (NOT (Task LIKE '%x%') OR Task IS NULL) AND (NOT (ExpressEmail IS NULL OR ExpressEmail = '')) AND (NOT (WebSite IS NULL OR WebSite = '')) AND (Mail17Date IS NULL OR Mail17Date = '') AND (Mail18Date IS NULL) AND (Mail1Date <= '20080421') AND (NOT (RECORDOWNER = 'lbm') OR RECORDOWNER IS NULL)
How would i get the part in red to say mail1date less than or equal to 42 days back from todays date?
I need to select all database names that are not contained in table tsm.dbo.DB_NOT_to_Backup, but are present in master..sysdatabases. Can this be done using not equal joins?
I tried the query below, but it got me nowehere:
select a.name from master..sysdatabases a inner join tsm.dbo.DB_NOT_to_Backup c on a.name<>c.DBname order by a.name
I have created an SQL server table in the past on a server that was all case sensative. Over time I found out that switching to a server that is not case sensative still caused my data to become case sensative. I read an article that said you should rebuild your master database then re-create your tables. So after rebuilding the master database, a basic restore would not be sufficient? I would have to go and manually re-create every single table again?
I have table 'A' containing Company information including the company zip code. I have table 'B' which is a table of ZipCodes. How can I get a result set of all the Zip Codes that exist in 'A' but not in 'B'? Thank you
Hi, I have a stored procedure that returns a string. How do I assign it to a variable? I have currently exec myStoredProc '2' and I want something like declare @myVar varchar(25) set myVar = exec myStoredProc '2'
But I can' seem to figure out the syntax for assignment. Thanks
I'm having a problem with what should be a simple TSQL statement. I have a table which has a datetime field updated. After the update if I type
select * from patient_medication where rec_status_date = '11/10/2000' it returns the rows I want.(All the dates have a time of 00:00:00.000. But if I type select * from patient_medication where rec_status_date <> '11/10/2000' or select * from patient_medication where rec_status_date != '11/10/2000'
The rows that have a value are returned, but none of the null values are returned. Will nulls not work with this comparison? Thanks