sql

what's the escape sequence for hyphen (-) in PostgreSQL

Hi, I'm trying to rename a database to a name with a hyphen (-). ALTER DATABASE one RENAME TO one-two; And psql returns an error: ERROR: syntax error at or near "-" What should I use as an escape sequence for "-" character or what's the way to do the above? Note: I've tried the '\-' and didn't work as well. Thanks. ...

SQL: How to create view from a recursive query?

Question: I have a view which I want to derive from a recursive query. The query is of the same structure as this one here: http://forums.asp.net/t/1207101.aspx And represents a treeview as an ordered dataset. How can I create a view which does this: ;WITH Tree (ID, [NAME], PARENT_ID, Depth, Sort) AS ( SELECT ID, [NAME], PARENT_I...

Why does casting AVG(intger_column) as DECIMAL return a minimum of six decimal places?

Consider this query: WITH Scores (score) AS ( SELECT CAST(score AS INTEGER) FROM ( VALUES (0), (10), (10) ) AS Scores (score) ) SELECT AVG(CAST(score AS DECIMAL(19, 8))) AS precision_eight, AVG(CAST(score AS DECIMAL(19, 7))) AS pr...

Simple distance query is not working

I have latitude and longitude defined as decimal(9, 6) in my zip code table, per the zip code database company's instructions. I take a lat/lon feed it to a function in my c# program and it gives me lat/lon in boundaries for getting a list of lat/lon from the database (radius distance) The sql code does not work with the longitude. SE...

Select that returns based off two columns as a Key

I have a problem that I can't seem to solve. What I am trying to do is find unique columns based in table A that doesn't exist in other table B and store them in table B. Something on the lines of SELECT DISTINCT(A.FNAME), A.LNAME from ADDRESS A WHERE NOT EXIST (SELECT DISTINCT(B.FNAME),B.LNAME ...

DATEPART as a parameter

I'm guessing this is not possible since the engine doesn't like it, but is there a way (barring dynamic SQL) to pass in a DATEPART as a parameter to a procedure? ...

SQL to dBase Query Translator?

Is anyone aware of an application that will allow me to convert some existing SQL queries into dBase? I have an application that I need to program but it prefers dBase queries to SQL queries. I've written a working SQL query, but now need to figure out what the query would look like in dBase. ...

SQL Server: how to get a database name as a parameter in a stored procedure

I'm trying to create a simple stored procedure which queries a sys.tables table. CREATE PROCEDURE dbo.test @dbname NVARCHAR(255), @col NVARCHAR(255) AS SET NOCOUNT ON SET XACT_ABORT ON USE @dbname SELECT TOP 100 * FROM sys.tables WHERE name = @col GO This does not seem to work cause I should put GO ...

SQL / Doctrine : Left Join problem

Hello, Currently this function works : it displays for a specific game, how many jobs there are. The problem : If there is no job, the game does not appear on the list. How to display the game even if there is no job attached ? Thanks public function getWithGames() { $q = $this->createQuery('c') ->leftJoin...

Build temporary table with dynamic sql in SQL Server 2008

To make a long story short... I'm building a web app in which the user can select any combination of about 40 parameters. However, for one of the results they want(investment experience), I have to extract information from a different table and compare the values in six different columns(stock exp, mutual funds exp, etc) and return only...

str_replace in SQL UPDATE?

Here's a sample table: name | picture John S. | http://servera.host.com/johns.png Linda B. | http://servera.host.com/lindab.png ... Let's say there are several hundred more records. Let's also say we moved servers from "servera" to "serverb". Would it be possible to go into this table with one query to rename the c...

T-Sql - Pivot Error

I have the following code in a T-Sql query, I am getting the following error message and I am not really sure what is causing the error. I am writing the Pivot statement to be dynamic b/c I do not know the columns that will be returned. Error Message: Msg 8156, Level 16, State 1, Line 9 The column 'Title - Endorsement Fee / END8' was s...

Foreign Keys in Log Table

Hey, so a bit of a design question here: I am working on a project that requires me to keep track of when a user performs an Insert, Update, or Delete. The thing I am trying to decide on is whether to have the Logs table reference the Primary Key of the affected row or some other identifier. In order to log a deletion when using Forei...

What would this SQL query look like in dBase?

Can someone help me translate this query into a dBase equivalent expression? select top 300 * from CONTACT1 c1 left join CONTACT2 c2 ON c1.ACCOUNTNO=c2.ACCOUNTNO where KEY1='10INQA' and KEY2='2011 Fall' and c1.ACCOUNTNO NOT IN (select accountno from CONTSUPP where RECTYPE='E') ...

Stored procedure scope: using sp from another database

I hava a stored procedure named PROC_fetchTableInfo (simple select from sys.tables) in a database named Cust I would like use this procedure in a another database named Dept I tried to execute this sp in this database using command EXECUTE Cust.dbo.PROC_fetchTableInfo but as a result I get tables from the database Cust. How to g...

How to override precedence in SQL*Plus?

select * from tab1 minus select * from tab2 union select * from tab2 minus select * from tab1 How can I make sure that it is parsed as this? ((select * from tab1 minus select * from tab2) union (select * from tab2 minus select * from tab1)) ...

Complex SQL I don't want to convert in a stored procedure

Hi all! I would like to have your assistance in order to know if it's possible to achieve the desired query without needing a stored procedure. I will try to explain myself as good as I can. I have a similar structure to this: PK / FK / DateTime / Value 1 / 68 / 10:30 / 60.5 2 / 68 / 09:30 / 10.5 3 / 61 / 05:30 / 01.0 4 /...

Drop Unique Constraint on Table Column without Knowing the Constraint Name

In Oracle 10g, how can I drop a unique constraint on a column without knowing the name of the constraint (e.g. a system generated name, which won't necessarily be the same across database instances)? Dropping and recreating the table isn't an option. Is it possible? ...

What is the most efficient way in T-SQL to compare answer strings to answer keys for scoring an exam

These exams typically have about 120 questions. Currently, they strings are compared to the keys and a value of 1 or 0 assigned. When complete, total the 1's for a raw score. Are there any T-SQL functions like intersect or diff or something all together different that would handle this process as quickly as possible for 100,000 examinee...

Combine sql scripts

I'm trying to write a sql script that loops through every table in my database and generates an xml schema based on the structure only, not the data. So far I have this script that loops through the tables and finds which column is the primary key: select kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, tc.CONSTRAINT_TYPE, kcu.CO...