Any Way To Make This Shorter?
Aug 1, 2006
I have a T-SQL query that is used to pull up some data for once-a-day export, just out of curiosity more then anything, is there a way to make this shorter?
SELECT DISTINCT
u.userId,
u.lastName,
u.firstName,
u.address1,
u.address2,
u.city,
u.state,
u.zip,
CASE WHEN u.UserClassID_fk BETWEEN 1 AND 3 AND COALESCE(u.RetailerNumber_fk,0)= 0
THEN 'Corporate'
WHEN u.UserClassID_fk BETWEEN 1 AND 3 AND COALESCE(u.RetailerNumber_fk,0)<> 0
THEN 'RETAILER'+ CONVERT(varchar, COALESCE(u.RetailerNumber_fk,0))
WHEN u.UserClassID_fk BETWEEN 4 AND 8 AND COALESCE(p.plantCode,ap.plantNumber_fk,ps.plantNum ber_fk,0) = 0
THEN 'Corporate'
WHEN u.UserClassID_fk BETWEEN 4 AND 8 AND COALESCE(p.plantCode,ap.plantNumber_fk,ps.plantNum ber_fk,0) <> 0
THEN 'PLANT'+ CONVERT(varchar, COALESCE(p.plantCode,ap.plantNumber_fk,ps.plantNum ber_fk,0))
WHEN u.UserClassID_fk BETWEEN 9 AND 14 AND COALESCE(p.regionNumber_fk,rg.regionNumber,0) = 0
THEN 'Corporate'
WHEN u.UserClassID_fk BETWEEN 9 AND 14 AND COALESCE(p.regionNumber_fk,rg.regionNumber,0) <> 0
THEN 'REGION'+ CONVERT(varchar, COALESCE(p.regionNumber_fk,rg.regionNumber,0))
END CPGkey
FROM [...] JOIN [...]
I'm hoping there is something in a way of....
If userClass Between 1 and 3 Then
IF COALESCE(u.RetailerNumber_fk,0)= 0 Then 'Corporate'
ELSE 'RETAILER'+ CONVERT(varchar, COALESCE(u.RetailerNumber_fk,0))
End IF as CPGKey,
Thanks in advace.
View 1 Replies
ADVERTISEMENT
Apr 3, 2008
I need query like this that make a row of numbers (in one execute)
SELECT a from (
SELECT -1 a UNION ALL
SELECT -2 UNION ALL
SELECT -3 UNION ALL
SELECT -4 UNION ALL
SELECT -5 UNION ALL
SELECT -6 UNION ALL
SELECT -7 UNION ALL
SELECT -8 UNION ALL
SELECT -9) t;
View 4 Replies
View Related
Mar 9, 2012
I have a table to which data is being imported via bcp. When I get this data there are some fields nvarchar that has some decimal, for instance 1098.000000 is always 6 zeros..What I want to achieve is to make the above number only 1098
So what I did was this:
LEFT (AMOUNT, LEN(AMOUNT)-7) AS AMOUNT
But i get an error saying invalid lenght parameter passed to the left or substring
View 2 Replies
View Related
Jan 16, 2008
Hello,
Today I ran a query from my ASP page that returned with an error message stating my command had timed out. Fine. I searched the forums (thank god for the internet), and added cmd.CommandTimeout = 120. Great! That solved my problem.
My question is that from everything I read, the command timeout is set to 30 seconds by default. Yet my query runs in 5 seconds or so with the property set, but wouldn't run at all without it. Is it possible my command timeout was set somewhere else to less than 30 seconds? It's almost as if the server THOUGHT it would take longer than 30 seconds, and gave up, before actually running the query (which has all needed indexes to make the query fast). I just don't understand why I was getting this error for what turns out to be a 5 second query. When I shorted the query to 4 seconds or so (reducing the date range for the report), it ran fine in 4 seconds without the timeout error. So basically, going to 5 seconds or more caused this error. Where are the other places that the CommandTimeout property might be set? On the SQL Express database itself? I didn't see any ASP.NET config properties addressing it. Is there somewhere else I can look?
Thanks!
Michael
View 5 Replies
View Related
Jun 8, 2001
Hello Folks,
I'm looking for the group's collective wisdom on the following issue, I'm sure many have been confronted with it.
I'm designing a db for a website that will utilize SQL Server 7. I have few tables that I think will grow very large row wise and that will be written to an read from frequently. I sense that I might be able to get better perfomance out of the system if I split many of these large tables up (row wise) into many smaller tables.
For example, The website that I'm working on has a "MailingAddresses" db table that contains the mailing addresses for the sites users (subscribers and other misc users - 1 record per user, anticipating 70,000+ users). Each user can update their record, and there will be frequent queries to the tables to get addresses for both internal admin use and for display on public webpages.
The website has five distinct sections that are in some sense like five distinct websites. Each user belongs to only one of these sections and they won't migrate between sections. Therefore I'm considering breaking up the one large "MailingAddresses" tables into five smaller tables, one for each section, i.e., "MAddressesSectionA", ..., "MAddressesSectionE".
These tables will have the same fields and constraints. Also of course there would be the same number of reads and writes in total with the five smaller tables as compared to that for the one big table. Also the combined size of the info in the five smaller tables would be the same as that for the one large table.
Though it's going to more of a pain to manage five tables versus one, I have a hunch it might be easier for the dbms to handle reads and writes with five smaller tables than with one large table...
...Of course this is true when queries are mainly respective to users from just one or two of the sections (as might be the case) - the big table then has the overhead of the address records for users in the other sections. BUT is there any benefit with the five smaller tables route when they are all frequently accessed??? Sure each select query has fewer records to go through, but with all five tables in play the dbms has to deal on average with the same amount of info as in the one big table.
What do you folks think, to divide or not to divide the big table(s) up row wise into smaller tables?
I guess the issue is summed up in this question: In general, can a dbms better handle in memory, and more quickly write to and query access - one BIG table with a+b+...+n records, or N smaller tables with a, b,...,n records respectively?
Thanks for the collective wisdom, - Jerry
View 2 Replies
View Related
Jan 8, 2007
Exception ex.Message I am getting in debug mode calling AcquireConnection is much shorter than the logged message in run time because all the errors come to output window in debug. Is there any way to get all these errors description from exception object in debug time to be able to parse it?
Thanks.
View 5 Replies
View Related
Jun 30, 2006
Can somebody help me convert this SQL2000 Function TO C# Function pleaseCREATE Function dbo.CalculateNextRentDate ( @Rent_Payment_Date datetime, @Frequency varchar(50) , @Number int)RETURNS DATETIMEASbeginDECLARE @NextPaymentDate DatetimeSET @Number = @Number - 1.IF @Frequency = 'month' IF @rent_payment_date = dateadd(month, datediff(month, 0+@Number, @rent_payment_date) + 1, -1) BEGIN SET @NextPaymentDate = dateadd(month, datediff(month, 0, @rent_payment_date) + 2, -1) END ELSE BEGIN SET @NextPaymentDate = dateadd(month, 1+@Number, @rent_payment_date) END return @NextPaymentDateend
View 1 Replies
View Related
Aug 16, 2007
Our company has its Departments And Services.
Now We are making it online.
Both have separate email list, phone numbers, and more.
Will I make one table and adds the field Type (Values: D or S).
Or make them separate.
Remember one thing If we merge them then Email And PhoneNumber Table will also me merge
other wise they will also separate.
What is better.
View 13 Replies
View Related
Mar 29, 2004
How to make our own DTS package I have to split the data on my own.. Data is very biig almost 30 to 40 million. I need to splitt them into 10k chunks in database and with my desired table name.
Waiting for a +tive reply.
Regards
Shani ;)
View 6 Replies
View Related
Feb 12, 2008
hi i have a tabel in my database i tray to generat report for this tabel
this tabel have all this fields:
company_id
emp_no
seq_no
interval_date
in_time
in_type
out_time
out_type
wage_code
status
i want all his colums can be search
but with company_id
like if he enter company_id and emp_no okay give him result if he enter Company_id and interval_date okay give him result
i write this
Code Snippet
SELECT company_id, emp_no, seq_no, interval_date, in_time, in_type, out_time, out_type, wage_code, status
FROM interval
WHERE (company_id LIKE @CompanyID) AND (emp_no LIKE @EmployeeID) OR
(company_id = @CompanyID) AND (interval_date = @IntervalDate) OR
(company_id = @CompanyID) AND (in_time = @InTime) OR
(company_id = @CompanyID) AND (in_type = @InType) OR
(company_id = @CompanyID) AND (out_time = @OutTime) OR
(company_id = @CompanyID) AND (out_type = @OutType) OR
(company_id = @CompanyID) AND (wage_code = @WageCode) OR
(company_id = @CompanyID) AND (status = @Status)
but in report preview it tell me i must enter intrevalDate ?
View 8 Replies
View Related
Apr 5, 2006
Hi have have this problem, I have a table called PABX that has all the callings registry and what I need to do is for each client(PABX.cod_client) I have 2 types of calls (VC1, VC2) , and for these types I need to select all the registries chaging the dialed number(PABX.NRTELEFONE) for the new one (TROCAR.NRTELEFONE) and for those client that doesn't need to change select the PABX.NRTELEFONE
is it possible through SQL Server 2000(via stored procedure) or I'll need to do it by my application using a vector ?
Thanks
View 4 Replies
View Related
Jan 25, 2008
hiiiiiiiiii I am creating a web application using vb.net in which i m using the concept of classes. now i am done all the code for inserting the values in the database using the class but it is difficult to fetch the values from the database using select command and sending them to a WebForm . i want to know how i send send the values coming from the select command to a datagrid or another web controlif possible provide me a sample code thanks for your help
View 2 Replies
View Related
Feb 2, 2006
hello all i have a 2 questions hope that u can help me my first question is: in ms access there was a data type named yes/no and it was a checkbox is there a checkbox data type in sql server 2005?
my second question is i need the connection code between asp.net 2005 and sql server 2005 i searched in here for that code but i got more confuse i found two codes and both are not working so hope u can give me the right connecting code. that's all thanks
View 11 Replies
View Related
Sep 28, 2001
hi all,
Now i want to log some information (e.g.time,count...)during SP execution,how can i do it in Sql?
Thanks
View 1 Replies
View Related
Sep 28, 2005
Can anyone tell me how I would go about making my SQL server accessable from the Internet, or know of any good tutorials to get me started, I haven't had much luck looking on google.
I need to access an SQL database from one server on another server for a web application.
Thanks
View 4 Replies
View Related
Jun 28, 2004
Hi guys, I have a car_race table which has these fields
car_id int
race_id int
b_car_won varchar // can have 'y' or 'n'
I need to know if the car lost the 1st race but won the next race
And example of that table for car_id 1:
car_id 1
race_id 1
b_car_won 'NO'
car_id 1
race_id 2
b_car_won 'YES'
Now this is the tricky part, the database has some data integrity issues, so this can occur:
car_id 1
race_id 1
b_car_won 'NO'
car_id 1
race_id 3
b_car_won 'YES'
So I cant used a fixed race_id value, need to use the race_id > 1 to know whats the next race. But this raises another issue if I have this in the database:
car_id 1
race_id 1
b_car_won 'NO'
car_id 1
race_id 3
b_car_won 'YES'
car_id 1
race_id 4
b_car_won 'YES'
If I query I'd get 2 rows where race_id > 1. And I only need the first one, because 3 is the next race.
I need to fetch in a single row if possible, the result of the 1st race and the 2nd race. How can I do this?
View 11 Replies
View Related
Dec 15, 2004
Hello!
I have two tables
users and pictures.
table users have a clustered (PK) index on userid
table pictures have a clustered (PK) index on userid
when I do this query:
"select userid from pictures where userid=123"
then It will do a clustered index seek
But If I do any of those:
"select t2.userid from users t1 left join t2 on t1.userid = t2.userid"
or
"select (select userid from pictures where usedid = t1.userid) from users t1"
It will do a clustered index scan.
How can I force it to seek my index instead of scan?
Thanks!
View 1 Replies
View Related
May 26, 2006
i want to make a database for pharmacy using sql server and VB.NET
so any body can give me illuminations
thanks in advance
george albert
View 4 Replies
View Related
May 29, 2007
I need to restore a copy of database to a new db name
i'm trying by creating a new db and restoring by doing
RESTORE DATABASE [userr] FROM DISK = N'D:sqlbackupsuser.bak' WITH FILE = 4, NOUNLOAD, STATS = 10
GO
i'm getting an error
Msg 3154, Level 16, State 4, Line 1
The backup set holds a backup of a database other than the existing 'userr' database.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
What's the best way to do this?
View 10 Replies
View Related
Jan 28, 2015
I thought this would be somewhat easy but I'm having trouble with this one. I have a statement that if 'ACTLABCOST' or 'ACTMATCOST' has a value of 0.00 then I need to make it .00.
Here's the statement:
Select
CONVERT(VARCHAR(10), xn_approveddate, 101) + ' ' + convert(VARCHAR(8), xn_approveddate, 108)as "Approved Date",
WORKTYPE,
[Code]....
View 5 Replies
View Related
Jan 31, 2007
If following code, it is ok if I execute part1 and q1 together.
but if I try to execute Q2, I got error
Server: Msg 137, Level 15, State 2, Line 4
Must declare the variable '@str'.
I guess I have to execute part1 and Q2 at same time.
Is there a way to avoid that. I mean after I execute part1 @str will be kept in memory, and I can execut q2 without a problem? (like in SAS)
Thank
/* Part1*/
/* how to make @str global*/
declare @str nvarchar(20);
set @str='%subway%';
/*Q1*/
select *, case regionname when 'telesales' then 't' else 'o' end as rn
from dbo.RPT_ContractDetails
where businessname like @str
/* Q2*/
select contracttypename,regionname, count(*)as cou, sum(fundingamount)as Dollar
from dbo.RPT_ContractDetails
where businessname like @str
group by contracttypename,regionname
View 3 Replies
View Related
May 5, 2007
Hello!
I would like to backup a database through t-sql into a file.
I do this :
EXEC sp_addumpdevice 'disk', 'mydb','c:mydb.bak'
it tells "Command(s) completed successfully."
but I can not find the file c:mydb.bak ...
but if I do it again I get :
"Msg 15026, Level 16, State 1, Procedure sp_addumpdevice, Line 74
Logical device 'mealmanager' already exists."
:-(
Any idea? I simply want to get a .bak file of the db, that's all.
Thanks a lot for any feedback!
Regards,
Fabianus
my favorit hoster is ASPnix : www.aspnix.com !
View 12 Replies
View Related
May 29, 2007
hello !
Could someone tell me which collation I have to use for a text field with a unique index, but which should make the diffrence between œ and oe. (Latin1_General_CS_AS does not make a diffrence between them)
Thanks a lot for any feedback !
Regards,
Fabianus
my favorit hoster is ASPnix : www.aspnix.com !
View 4 Replies
View Related
Aug 28, 2007
select contractnbr,affcontractnbr,tdd,affrtramount,affturn,
pt=datediff(d,afffirstfundingdate,affRTRBalanceZeroDate),ratio=datediff(d,afffirstfundingdate,affRTRBalanceZeroDate)/afftrun
from #targetfinishedaff
datediff(d,afffirstfundingdate,affRTRBalanceZeroDate) appears two times in this code, Is there a way to make it appear only once?
Thx
View 1 Replies
View Related
Jan 31, 2008
I have this code:
USE BDPrincipal
GO
IF OBJECT_ID(N'aquery') is not null
DROP FUNCTION aquery
GO
CREATE FUNCTION aquery()
Returns nvarchar(500)
as
Begin
Declare @var nvarchar(500);
Set @var = 'Select Distinct Description from dbo.tblScanners';
Return @var
end
GO
exec sp_executesql aquery;
I created that code to prove if it works.
But the result doesn´t appear, and the message is:
Command(s) completed successfully.
I need it to work.
Because I need to create a very dinamic query.
Please help me!
View 4 Replies
View Related
Feb 14, 2008
I want to make query for average sales price, the records are like this
item, Sales_qty, UnitPrice
A, 10, 1000
A, 20, 1100
A, 5, 1500
B, 1, 1000
B, 2, 1200
and display like this
A, 35, 1128 (35 FROM total qty, 1128 from average sales price)
B, 3, 1133
Thanks in advance
View 8 Replies
View Related
Feb 26, 2008
i was wondering if anyone could help me write all this into one or two quries...
alter table testdata add areacode varchar(3),
countystate varchar(35),
citystate varchar(30),
county_name varchar(30),
ltv float
go
UPDATE t1
SET t1.county_name=t2.county
FROM testdata t1
INNER JOIN master.dbo.zip t2
ON t1.statecode+t1.countycode=t2.fips
go
update t1 set
maritalstatus = case maritalstatus
when '1' then 'M'
when '2' then 'S' end,
homeowner = case homeowner
when '1' then 'R'
when '2' then 'T'
when '3' then '9'
when '4' then 'H' end,
areacode = substring(phone,1,3),
citystate = rtrim(city)+' '+state_abbrv,
countystate = rtrim(county_name)+' '+state_abbrv,
ltv = case when estcurhomeval >0 then cast(oo_mtg_amnt as float)/cast(estcurhomeval as float) end
from testdata t1
go
basically, i have to run this against some fairly large tables, so i want to try and get it down to one pass,
instead of three.
Thanks in advance!
View 5 Replies
View Related
Jun 27, 2007
Hell All,To reproduce one of our cusotmer's probem, I need to make the SQL torun for more than a minutes before it returns the result set. I do nothave large amount of data in the database to simulate the dealy.Is there a way in SQL to cause the delay while returning the resultsetThanks for the help.RegardsRaj
View 7 Replies
View Related
Jul 20, 2005
I have two SQL query that I would like to make in to one, if possibleHere is the first one: (Query4)SELECT rooms.DESCRIPTIO, rooms.ID, bookings1.DATEFROM bookings1 INNER JOIN rooms ON bookings1.ROOMID = rooms.IDWHERE (((bookings1.DATE) Between #4/3/2004# And #4/9/2004#));And the second one:SELECT rooms.ID, rooms.DESCRIPTIO, Query4.ROOMIDFROM rooms LEFT JOIN Query4 ON rooms.ID = Query4.ROOMIDWHERE (((Query4.ROOMID) Is Null));Is it possible to make one query of this two?With the same result.
View 3 Replies
View Related
Apr 12, 2007
I am wanting to be able to take my work home with me, problem is I can access the server from there.
Is it possible to work with a local instance on my laptop, then when completed transfer it to another instance on a different pc?
Meaning, talbes,queries,etc.
Thanks
Davids Learning
View 11 Replies
View Related
Mar 18, 2008
I have inherited a SQL 2005 server with a few small databases on it. There's a maintenance plan here that doesn't seem to make a lot of sense to me. Can anyone comment:
Every Sunday at 4:00 AM
1. Reorganize index on All user database Tables and Views - compact large objects.
2. Rebuild index on local server connection, All user databases, Tables and view, Original amount of free space.
3. Shrink database. All user databases. Limit 100MB.
I'm confused a little about item 3. Won't a shrink be kind of useless after all of the work that goes on in steps 1 and 2. When I ran this manually, the transaction logs jumped significantly.
Thanks in advance for your help.
Dean
View 5 Replies
View Related
Jan 9, 2008
I havea textbox for searching by patient name but now and though I am searching with LIKE% i can only search for exact string, for example if user typed JASSIM it will search for field full_name LIKE '%JASSIM%" ad if user typed JASSIM RAHMA it will search for full_name LIKE '%JASSIM RAHMA%' but i want to search if the field CONTAINS '%JASSIM% AND %RAHMA% when the user types JASSIM RAHAM in the textbox
View 4 Replies
View Related
Apr 9, 2006
Hi.
I am almost wrapping up a project of my own and I will have the option to restore the database, so dropping the existing one and then running the SQL script to create the database, tables, stored procedures and so on.
My question is, (using .NET 2.0 C#) - what is the best way to resolve the "cannot drop database as it is currently in use" message, or some message of the similar sort?
Even though I specified "USE MASTER" in the commandText string, there will be somewhere, in a situation, where the database will still be in use.
How can I forcefully make it NOT in use, drop it and do other things I like to?
What is the best way?
Many thanks!
View 9 Replies
View Related