sql

How to Use 'Like' with a parameter

I want to search for a number embedded in a string in a field in our log table using a parameter. select * from vwLogs where log_time >'02/24/2009' and message like ('%2009022508241446%') I know how to use parameters when the where clause is an equals sign but not sure how to do it with 'Like' this doesn't seem right WHERE message l...

What is "runaway query"?

In the context of SQL, what does a runaway query mean? Does it mean any query that runs wild when it takes too long? Or does it mean when it has some side-effects due to triggers? ...

joins on ints vs joins on chars (or varchars)

Are there any performance drawbacks in SQL to joining a table on a char (or varchar) value, as opposed to say joining on an integer value? ...

How does SET NOEXEC ON work?

I have the following code: -- start of code set noexec off declare @requiredVersion int declare @currentVersion int set @requiredVersion = 5 set @currentVersion = 4 if (@currentVersion < @requiredVersion) begin print 'Please update your DB to version 5 before running this script.' set noexec on end go -- print 'Dummy' insert...

Forum Structure Get Last Post

My english is not so good. I basicaly want to show the date when that last post was posted in a forum. This is my forum shema: `forum_id` int(11) NOT NULL auto_increment `forum_name` varchar(255) NOT NULL `forum_description` text NOT NULL `forum_order` int(11) NOT NULL `thread_id` int(11) NOT NULL auto_increment `thread_tit...

SQL to find the difference between two rows

Hi I have an Informix database containing measured temperature values for quite a few different locations. The measurements are taken every 15 min for all locations and then loaded with a timestamp into the same table. Table looks like this: locId dtg temp aaa 2009-02-25 10:00 15 bbb 2009-02-25 10:00 20 cc...

Optimizing a Query to Build Averages

Hello All, I recently built a query in SQL that I can use to look at our payment log and find out an average number of payments made per hour over a period of time. I'm sure there are third party reporting applications that are much more suited for doing the type of calculation I'm trying to do, but just for fun, I'm kind of curious t...

What is the best way to escape non-format characters in Oracle's to_char?

I am trying to print a date in a select statement, but I need to add a letter to the output: to_char(date_updated, 'YYYY-MM-DDTHH:mm:ss') Oracle does not like the T. I just want the T to be output like the colons and the dashes. Can I escape it with a backslash or something? ...

Can a foreign key reference a non-unique index?

I thought a foreign key meant that a single row must reference a single row, but I'm looking at some tables where this is definitely not the case. Table1 has column1 with a foreign key constraint on column2 in table2, BUT there are many records in table2 with the same value in column2. There's also non-unique index on column2. What does ...

Optimizing "ORDER BY" when the result set is very large and it can't be ordered by an index

How can I make an ORDER BY clause with a small LIMIT (ie 20 rows at a time) return quickly, when I can't use an index to satisfy the ordering of rows? Let's say I would like to retrieve a certain number of titles from a table 'node' (simplified below). I'm using MySQL by the way. node_ID INT(11) NOT NULL auto_increment, node_title VAR...

Parsing SQL code in C#

i want to parse the SQL code using C#. Specifically is there any parser freely available which can parse the SQL code and generate a tree or any other structure out of it? it should also generate proper tree for nested structures. As well as it should return to me which kind of statement the node of this tree represents. e.g. if the ...

Is it possible in SQL to match a LIKE from a list of records in a subquery?

Using these tables, as a sample: Table CodeVariations CODE ----------- ABC_012 DEF_024 JKLX048 And table RegisteredCodes CODE AMOUNT -------- ------ ABCM012 5 ABCK012 25 JKLM048 16 Is it possible to write a query to retrieve all rows in RegisteredCodes when CODE matches a pattern in any row of the Cod...

What is a simple way to combine grouped values in one field?

I mean: Table PHONE_CODES: ID CODE_NAME PHONE_CODE 1 USA 8101 2 USA 8102 3 PERU 8103 4 PERU_MOB 81031 5 PERU_MOB 81032 And I want via select to get something like this: CODE_NAME ZONE_CODES USA 8101; 8102; PERU 8103 PERU_MOB 81031; 81032; I could get it via the functio...

Displaying images from albums

Hello, I have a website where users can create albums and then add images to those albums. I then have a section on the page where I display all images added by the users. You can see it here: http://www.mojvideo.com/slike The problem is that we have a few users who upload a lot (anything between 20 to 100) of images at once and they k...

How can you name the Dataset's Tables you return in a stored proc ?

I've got the following stored procedure Create procedure psfoo () AS select * from tbA select * from tbB I'm then accessing the data this way : Sql Command mySqlCommand = new SqlCommand("psfoo" , DbConnection) DataSet ds = new DataSet(); mySqlCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter mySqlD...

What's the preferred way to return an empty table in SQL?

I know I can return an empty table using the following query : select * from tbFoo where 1=2 but that code doesn't look nice to me. Is there a 'standard' way of doing this? If you're wondering why I want to do such a strange thing, it's because I can't name the datatables I return from a stored procedure, so I need empty placeholde...

Duplicate Count(Distinct [f]) in MS Access

Is there a quick way to duplicate the effect of the Count(Distinct [f]) in MS Access? For example: Data Table for a single referral (there are a few thousand of these in the real data): | Referral ID | Assessment Date | Assessment Team | Service Provided | Service Team | 1 | 02/01/2008 | AAA | BBB ...

For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC

I want to find the highest AutoIncremented value from a field. (its not being fetched after an insert where I can use @@SCOPE_IDENTITY etc) Which of these two queries would run faster or gives better performance. Id is the primary key and autoincrement field for Table1. And this is for Sql Server 2005. SELECT MAX(Id) FROM Table1 SELEC...

Pivoting tables

Hi all, I have a table like this: serialnumber partnb id actual nominal 1 1 AGR 15,2176803 15,2 1 1 APR 5,8060656 5,8 1 1 DCI 61,9512259 62 1 43 AGR 15,4178727 15,4 1 43 APR 7,235779 7,2 1 43 DCI 52,0080535 52 2 2 AGR 15,2097009 15,2 2 2 APR 5,8009968 5,8 2 2 DCI 61,9582795 62 2 ...

Using SSIS to do a between query

I am wondering if there is a way of doing a between query join in SSIS without using a temp table on my server. Given two tables, Accounts and Groups. Accounts contains a list of Accounts with a an upper and lower range to define a list of customers. Groups contain all the customers. I want to be able join the tables so that I get ...