sql

Tool to convert MS-Access Database to SQL script

Hi, Our Software Package uses MS-Access Database, this database has been modified many times, so, it's a bit complicated to just see the files (relationship between tables are small hell), anyway, Is there a way or program to convert this mdb file to SQL Script (Any sort of SQL Script will do it for now). Thanks, ...

SQL JOIN : Technical name for table identifier

When I use A.column name, B.Column name where A = Table A and B = Table B , what is the technical name for the A.Column name? Is it a prefix, identifier or what else? ...

Count rows in an SQL table in O(1)

I understand the best way to count the number of rows in an SQL table is count(*) (or equivalently count(PrimaryKey)). Is this O(1)? If not, why not? Why not just implement a counter and return it for this specific query? Is it because this query is not a common use case? If the answers vary according to SQL engine, I'd like to hear...

How do I select only the latest entry in a table?

I have a 3 table SQLServer Database. Project ProjectID ProjectName Thing ThingID ThingName ProjectThingLink ProjectID ThingID CreatedDate When a Thing is ascribed to a Project an entry is put in the ProjectThingLink table. Things can move between Projects. The CreatedDate is used to know which Project a Thing was last moved too. I ...

Update a timestamp column

I need to all of timestamps in a column the same amount. I want to write an sql statement like: update sometable set timecol = timecol + <4 months>; ...

Which is better: Distinct or Group By

Which is more efficient? SELECT theField FROM theTable GROUP BY theField or SELECT DISTINCT theField FROM theTable ...

Database Bejewelled Puzzle- How does this solution work?

I'm interested in the programming challenge presented by the game Bejewelled. It seems like a simple game but programmatically it's more complex that it looks. In my search for hints on how the board is evaluated, I came across this QUIZ put on by the good folks at Simple-Talk. They have posted the winning answer, but I'm tarred and f...

Is there a way to search a SQL Query with Month Numbers, and not Month Name?

I am trying to use the actual numerical value for the month on a sql query to pull results. Is there any way to do this without having a function to change the numbers to actual month names, then back to month numbers? The following code works for Names, what works for numbers? datename(month,(convert(datetime,DTSTAMP)))= 'Octobe...

SQL query for finding representative rows in a table

Let's say I have a payments table like so: PaymentID INT, CustomerID INT, Value INT, PaidOn DATE And I want to run a query for the maximum payment made by each customer. Is this possible using a single SQL query, to avoid having to dig through every row that I'm not interested -- or worse, run a query for each customer? The best I ha...

Bitwise Ops

I'm hooked on bitwise for a new security paradigm I'm creating called VMAC. Variable Matrix Access Control. I want to do logical implication on to bit strings. (1) Before I reinvent the wheel, is there an established shortcut to emulate '->' (logical implication) using AND and OR and NOT or other basic SQL binary operators? (2) XNOR ...

SPARQL Query, select everything except things that match?

I am getting comfortable writing regular queries in SPARQL, but I'm still having trouble with fancier stuff. My latest problem is trying to select everything except stuff that matches the where clause. For instance, say I want to find all the husbands who like a car color that their wife doesn't like (I'm working on a proprietary model...

Does End Using close an open SQL Connection

If I wrap a SQLConnection in a Using, should I close it or does the end using handle it? using cn as new system.data.sqlclient.sqlconnection() cn.open '{do a bunch of other stuff with commands and datareaders here} cn.close 'Do I need this? end using ...

LEFT JOIN vs. multiple SELECT statements

I am working on someone else's PHP code and seeing this pattern over and over: (pseudocode) result = SELECT blah1, blah2, foreign_key FROM foo WHERE key=bar if foreign_key > 0 other_result = SELECT something FROM foo2 WHERE key=foreign_key end The code needs to branch if there is no related row in the other table, but couldn't...

How to get the record of a table who contains the maximum value?

Although this question looks simple, it is kind o tricky. I have a table with the following columns: table A: int ID float value datetime date varchar(50) group I would like to obtain the "ID" and "value" of the records that contains the maximum "date" grouped by the column "group". Something like "what is the newest value fo...

sql server replication - get last synchronization date from query

Does anyone know the query the last synchronization date from sql server (2008). It is the same information displayed in replication monitor, but I want to be able to get that date from a query. ...

Days Unavailable

I need a simple SQL to accomplish the below: Problem: When a petrol bunk runs out of fuel, the admin makes note of the DateTime (RunOutDate) when it ran out of fuel and notes also the DateTime (ResupplyDate) when the fuel supply was back on. I need to create a report on how many days the bunk ran out of fuel. eg. 1/1/1 10:10 to 1/1/...

Nested Select statement??

Hi I'm very new to sql but have been passed a job in which I need to query the db(MS SQL 2005) I need to return all workers where a HeadID is given.(tables below) So I need to get all the managers that match the HeadID and then all the workers that match those managers by ManagerID. How would I do this? Any help or any sql terminology th...

SQL expression problem

Below is my table, a User could have multiple profiles in certain languages, non-English profiles have a higher priority. +----------+--------+----------------+----------------+ |ProfileID |UserID |ProfileLanguage |ProfilePriority | +----------+--------+----------------+----------------+ |1 |1 |en-US |2 ...

Problem returning field in ms sql 2005 - System.InvalidCastException:

Hi, Using the code below, I am returning an nvarchar field from ms sql 05 and keep getting a System.InvalidCastException. vo.PlacementID = dr.IsDBNull(0) ? null : dr.GetString(0); The vo.PlacementID variable is of type String so there shouldn't be a problem. The values I am trying to return are like this (number, number, letter): 00F,...

Combo boxes with Hibernate

Im searching for the best way to mantain combo box values within a Mysql Database accessed by Hibernate objects. Currently we have the following table: CREATE TABLE COMBO_VALUES( KEY VARCHAR(5) NOT NULL, COMBO_TYPE VARCHAR(20) NOT NULL, VALUE VARCHAR(100) NOT NULL PRIMARY KEY(KEY,COMBO_TYPE) ); INSERT INTO COMBO_VALUES VALU...