sql

t-sql select question

Hi, I have a table in a sql server 2008 database that contains bunch of records as well as a date column. The date is inserted automatically when a new entry to the table occurs. So, it contains the date of the record that has been created. I am trying to run a query that would return me the earliest date and the latest date in this t...

How does one query for count() on a series of ranges at once?

I'm trying to compose a breakdown of forum ranks on a phpBB forum. Forum ranks are defined by your postcount: if you have between 1-9 posts you're level 1; 10-24 and you're level 2; and so on. (The maximum rank is anything above 2000 posts.) Right now, my forum rank statistics page is just doing a bunch of dumb queries: SELECT COUNT(...

Circular database relationships. Good, Bad, Exceptions?

I have been putting off developing this part of my app for sometime purely because I want to do this in a circular way but get the feeling its a bad idea from what I remember my lecturers telling me back in school. I have a design for an order system, ignoring the everything that doesn't pertain to this example I'm left with: CreditCa...

PLSQL - Triggers: Cannot modify a column which maps to a non key-preserved table

What I'm suppose to do is use a trigger to update the quantity sold in the inventory table when an order is placed in the concessions_sold table(an insert is used). I admit that I suck at PLSQL so I'm not sure if I'm going about this the right way. The error I'm getting is: SQL> insert into concessions_sold 2 values(33, 104, '26-Apr...

How to set the default value of an Access 2003 field using SQL?

How can I set the default value for a field using SQL in MS Access? I tried this but got a syntax error: CREATE TABLE HELLO ( MUN INTEGER NOT NULL, ADD CHAR(50) DEFAULT'16 asd ST.' ) ...

Why do SQL statements take so long when "limited"?

consider the following pgSQL statement: SELECT DISTINCT some_field FROM some_table WHERE some_field LIKE 'text%' LIMIT 10; Consider also, that some_table consists of several million records, and that some_field has a b-tree index. Why does the query take so long to execute (several minutes)? What I mean is, why doesnt it lo...

How to get linq to produce exactly the sql I want?

It is second nature for me to whip up some elaborate SQL set processing code to solve various domain model questions. However, the trend is not to touch SQL anymore. Is there some pattern reference or conversion tool out there that helps convert the various SQL patterns to Linq syntax? I would look-up ways to code things like the foll...

How to get first character of a string in SQL?

I have a SQL column with length of 6. Now want to take only first char of that column. Is the any string function in SQL to do this? ...

TSQL Return Guid that occurs the most in a column

If I have a table with two columns, e.g. Name, and ID, where ID is GUID's, how do I return the Guid value from Column 2 that occurs the most? ...

max count together in an sql query 2

This is referring to the question i asked before, and got a really quick answaer (http://stackoverflow.com/questions/751156/max-count-together-in-an-sql-query). The Problemset is similar, but the solution in the prevous question would force me to access a db in a loop which will cause performance problems. So what i have now, after some ...

Query similar to TOP 10 to select inbetween records?

I have 100 records in which i can select top 10 using the "TOP 10" in the query. Similarly is there anything to get the 20th to 30th record? ...

Regular expression to change user input

I am doing a search on my database. If the user enters eg. "foo bar" I want to display rows that have both "foo" AND "bar". My SQL syntax looks like this: SELECT * FROM t1 WHERE MATCH (t1.foo_desc, t2.bar_desc) AGAINST ('+foo* +bar*') How can I change the user input "foo bar" into "+foo* +bar*" using a regular expression in PHP? ...

Sort Postcode for menu/list

I need to sort a list of UK postcodes in to order. Is there a simple way to do it? UK postcodes are made up of letters and numbers: see for full info of the format: http://en.wikipedia.org/wiki/UK_postcodes But my problem is this a simple alpha sort doesn't work because each code starts with 1 or two letters letters and then is imme...

Which SQL query is better, MATCH AGAINST or LIKE ?

To search the database for rows that have both keywords "foo" AND "bar" in any of the columns "foo_desc" and "bar_desc", I would do something like: SELECT * FROM t1 WHERE MATCH (t1.foo_desc, t2.bar_desc) AGAINST ('+foo* +bar*' IN BOOLEAN MODE) or SELECT * FROM t1 WHERE (CONCAT(t1.foo_desc, t2.bar_desc) LIKE '%foo%') AND (CONCAT(t1.foo...

"Could not delete from specified tables" error only on published ASP.NET web site

I have written a simple delete query delete from mails While I execute I get no problem and the query runs fine, but when I publish the website I get an error stating: "Could not delete from specified tables" What might be the problem? I am getting this error when placed in IIS but when i run it in local drive through visu...

problem with Update in MySQL

According to the documentation, joins, when used with the update statement, work in the same way as when used in selects. For example, if we have these two tables: mysql> SELECT * FROM orders; +---------+------------+ | orderid | customerid | +---------+------------+ | 1 | 1 | | 2 | 2 | | 3 | ...

How to join two tables together with same number of rows by their order

I am using SQL2000 and I would like to join two table together based on their positions For example consider the following 2 tables: table1 ------- name ------- 'cat' 'dog' 'mouse' table2 ------ cost ------ 23 13 25 I would now like to blindly join the two table together as follows based on their order not on a matching columns (I ...

Datatable insertion in SQL using c#

How can we insert the values of datatable in SQL using c#? And the second question is how we can copy the SQL table into datatable variable? ...

Auditing SQL Server data changes

I'm looking at changing our Auditing process for our SQL Server 2005 databases and I came across Change Data Capture in SQL Server 2008. This looks like a good idea, and I'm tempted to try it, but before I do has anyone used it in a commercial environment and what are your thoughts? I noticed when I was reading about CDC in the MS hel...

Is too many Left Joins a code smell?

If you have for example > 5 left joins in a query is that a code smell that there is ... something wrong with your design? you're doing too much in one query? you're database is too normalized? ...