sql

Variables not recognized in PostgreSQL function subquery?

Working solution: CREATE OR REPLACE FUNCTION my_search(tsq tsquery, translation_id integer, lang regconfig, count integer DEFAULT 10, skip integer DEFAULT 0) RETURNS TABLE(id int, chapter int, number int, headline text) AS $$ SELECT id, chapter, number, ts_headline($3, text, $1, 'StartSel = <em>, StopSel = </em>'::text) FROM ( ...

SQL subqueries to try to get maximum difference of the same column in two tables

Hi all, I’m looking to get the max discrepancy between two tables per day, per id. I have the following data in a mysql database insert into test.foo values ('2010-01-10', 1, 10); insert into test.foo values ('2010-01-10', 1, 5); insert into test.foo values ('2010-01-10', 2, 10); insert into test.foo values ('2010-01-10', 2, 10); inse...

Query on datetime fields with milliseconds gives wrong result in SQL Server

I'm running into an odd bug using datetime fields in SQL Server 2005. The datetime field shows up with millisecond-level accuracy, but it looks like the milliseconds are not always used. Here's my test query: SELECT col1, YEAR(col1) AS yr, MONTH(col1) AS mn, DAY(col1) AS dy FROM mytable WHERE col1 >= '2009-12-31 00:00:00.0' AND col1 <= ...

Seemingly Simple Query in Pl Sql

I have a table "defects" in the following format: id status stat_date line div area 1 Open 09/21/09 F A cube 1 closed 01/01/10 F A cube 2 Open 10/23/09 B C Back 3 Open 11/08/09 S B Front 3 closed 12/12/09 S B Front My problem is that I want to ...

SQL queries through PYODBC fail silently on one machine, works on another

I am working on a program to automate parsing data from XML files and storing it into several databases. (Specifically the USGS realtime water quality service, if anyone's interested, at http://waterservices.usgs.gov/rest/WaterML-Interim-REST-Service.html) It's written in Python 2.5.1 using LXML and PYODBC. The databases are in Microso...

Force the Opening of the DataContext's Connection (LINQ)

Hi, When you create a datacontext, its connection is closed until you retrieve objects and it stays open when you retrieve objects in case you use deferred operators or late binding. Is it possible (in an extension method of the datacontext of not) to force the datacontext to open its Connection without querying LINQ with LINQ or doing...

Hashbytes comparison in stored proceduring not matching record

The password field in my user table (SQL Server 2008) is encrypted using HASHBYTES on insertion. I have a stored procedure with parameters for the username and plain-text password which does a SELECT using that username and the password sent through HASHBYTES, then returns the user record if it finds a match. The SP is always returning...

SQL multiple column ordering

I am trying to sort to multiple columns in SQL, and in different directions. coloumn1 would be sorted decending, and coloumn2 accending. How can I do this? ...

How the schema migration tools work?

I have db based application and I delete the schema & db content every time whenever there is any changes in the schema. Now it is in development mode. Soon we release the application in production. But we suspect there could be many changes after we release the application in production use. The software shall be installed on many loca...

How to sort through an association table in hibernate?

Given a representation of database tables where two entities are connected through a join table, is it possible to sort on the other entity specifying the sort order somehow through annotations? Consider, roughly, the following database tables: actor ( id, name ) credit ( actorId, movieId, roleName ) movie ( id, title, year ) And hi...

How do I handle duplicate entries in a single column database?

A user may enter keywords into a text field and separate the keys using comma. So the input may be bananas, apple, orange, pineapple. In my database, I have a table called keyword, and it has only one column keyword which also is the primary key. I add the keywords to the database, by $myArray = expload(',', $keywords). Then I loop thr...

Howto programmatically generate pretty printed SQL from .net?

The title says it all, is there a library or source code that's GPL that can turn a SQL statement into beautified HTML? I'd even settle for some PHP or Java code that I can port to .NET Tried google, but couldn't find any free sample code. Thanks. ...

How do I export this Unicode table as CSV in Perl 5?

I have a table that has some Unicode in it. I know that the Unicode data is fine as it comes out as JSON on our webserver just fine. But for some reason the CSV that I'm generating is ending up mangled. Here's our current code: my $csv = Text::CSV->new ({ eol => "\015\012" }); open my $fh, '>:encoding(utf8)', 'Foo.csv'; my $sth...

SQL Server Backup Files

Is the structure of SQL Server database backup BAK file similar to NTBackup BKF file? Is there any reference which can be redirected to understand the difference between NTNAckup and SQL Server Backup (*.bak) files? ...

Constructing a has-and-belongs-to-many query

Hi, I have a rails app (running on version 2.2.2) that has a model called Product. Product is in a has-and-belongs-to-many relationship with Feature. The problem is that I need have search functionality for the products. So I need to be able to search for products that have a similar name, and some other attributes. The tricky part i...

How do I order by on a varchar field that could contain numbers alphabetically?

I am sure that this must be quite a common problem so I would guess that Microsoft have already solved the problem. My Googling skills are just not up to scratch. I have a field that I want to order by, it is a varchar field, for example Q Num 10 Num 1 A Num 9 Num 2 F Now I would expect the result to be A F Num 1 Num 2 Num 9 Num ...

ordered SQL Select columnwise distinct but return of all columns

I have a little SQL Distinct puzzle that i cannot solve (or at least not in an very elegant way). I have two tables (try to ignore the simplicity of the example). I'm using MSSQL 2008 if that makes much of a difference. Table: Category | categoryId (uniqueidentifier) PK | | Name varchar(50) | Table: Download | do...

SQL Server: reasonable "auto increment" techniques?

I'm using SQL Server 2005 for the first time, having mostly worked with MySQL in the past. I'm used to using auto_increment to create unique IDs in tables. Anyway... I'm working in a java app, and need to do the following. Presume my table has two columns: itemID (int) and itemValue(int). This is basically what I want to do (the dbco...

Add a job step in SQL Server 2005

Hi, I have a questions about creating a SQL Job. Can somebody please let me know that how we can add a job step in the middle. Like if I want to add a step between step 2 and step 3. I would really apprecaite it. Thanks, ...

How to speed up a SQL Server query involving count(distinct())

I have a deceptively simple SQL Server query that's taking a lot longer than I would expect. SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT COUNT(DISTINCT(guid)) FROM listens WHERE url='http://www.sample.com/' 'guid' is varchar(64) NULL 'url' is varchar(900) NULL There is an index on guid and url. There are over 7 million ...