sql

SQL Server AS statement aliased column within WHERE statement

I want to execute a query in which I rename one of the columns using the 'AS' statement and reuse that aliased column name within the 'WHERE' statement. Below is an example: SELECT lat AS latitude FROM poi_table WHERE latitude < 500 The problem here is that SQL Server does not like this query because of the WHERE clause and the AS s...

Postgresql case and testing boolean fields

Hello to all. First: I'm running postgresql 8.2 and testing my queries on pgAdmin. I have a table with some fields, say: mytable( id integer, mycheck boolean, someText varchar(200)); Now, I want a query similary to this: select id, case when mycheck then (select name from tableA) else (select name from tableB) end ...

How do I join on "most recent" records?

I've got two tables in a SQL Server 2000 database joined by a parent child relationship. In the child database, the unique key is made up of the parent id and the datestamp. I'm needing to do a join on these tables such that only the most recent entry for each child is joined. Can anyone give me any hints how I can go about this? ...

How to attach a file to an email using SQL stored procedure?

Can't seem to find much information about how to attach a file that's been stored as a BLOB as part of an email. I know you can attach files from the file system (C:\temp...) to email that are being setup in DBMAIL or custom stored procedures. However, I haven't seen any references to attaching something such as a PDF that's been store...

SQL Set Column Equal to Value

I have the following query. I need Description to be set to the value of the long statement in the middle - the part beginning with CONVERT and ending with [Description]. The query I have now runs but the value of Description in the result is always NULL. Any ideas? select s.SectionId, s.Academic_Year [AcademicYear], s.Academic...

XML Query Converting Group by field to child elements

I want the following query to return the fields in a parent child format. Is this possible? Here's the query SELECT Field1, Field2, Field3 FROM ATable GROUP BY Field1, Field2, Field3 ORDER BY Field1, Field2, Field3 FOR xml auto Here's what I would like to get back (doesn't...

using references in MYSQL

i got an answer to another question here: http://stackoverflow.com/questions/3094495/db-schema-for-chats/3094915#3094915 it's a great answer, but i am not understanding the bit about references. i can do sql statements but i have never used references. what are they used for? how are they used? give an example please ...

SQL Server - need a SQL Query to identify/highlight specific changes in an audit table

Say I have some data stored in an audit table, where triggers on the main data table write all invoice record updates to this audit table. The audit table contains this data: InvoiceID CustomerID ItemSold AmountSold SalesPerson ModifyDate 1001 96 Widget 800 Robert 2001-1-1 1006 85 ...

Good embedded database for Qt?

I am looking for good embedded database that i can use for application developed using Qt. The applications target desktop users from various sites of a single large company. The database should be able to store data separately at each site and the data shall be merged with other sites as and when it is required. ...

Oracle-->SQL - forced conversion from non-unicode to unicode?

I have an ETL that is importing tables from Oracle to SQL 2008 using the OLEDB FastLoad. The data in Oracle is non-unicode. When the table is created in SQL it is created with unicode datatypes. For some reason the datatypes are being forced from non-unicode to unicode. Do any of you know of a way to stop this from happening? Possibly a ...

SQLite Syntax (order by causing error)

Looking for some syntax help for SQLite, a very odd thing is happening. I am running the query on an iPhone. Here is the query that fails: (Assume that the tables are correct, this runs great in the firefox sqlite plugin) select tcodes.DisplayOrder, StatusText, StatusCode from tcodes join tcode_transitions on tcode_transi...

add a temporary column with a value

I have a select statement like this select field1, field2 from table1 What I want is to have a newfield with only value "example". newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field. ...

I am trying to create this table in SQL Server 2008

CREATE TABLE DEPARTMENTS (Department_Id varchar(5) Primary Key NOT NULL, Department_Name char(20) NOT NULL, Manager_Id varchar(5) FOREIGN KEY REFRENCES EMPLOYEES, Location_Id FOREIGN KEY REFRENCES LOCATIONS) GO CREATE TABLE EMPLOYEES (Employee_Id varchar Primary Key NOT NULL, First_Name char(20) NOT NULL, Last_Name char(20) NOT NUL...

How to handle SQL query result caching

In a Perl program I cache SQL request result to speed up the program. I see two common way to do that: Create a hash using the query as index to cache result, like suggested here Create a hash with but 2 index, first is the list of used table, second is where clause I today used the 2nd option because it's easier to clean the cache ...

Correcting the SQL syntax?

Hi, Thanks very much for those responded. Is there a way in SQL server that takes the data from table1 and outputs the data like table2? Thanks! Table1: +---------+-----------+----------+------------------+ | Name | DOB | Agent ID | Agent Name | +---------+-----------+----------+------------------+ | subject | 4/20/196...

MySQL Rating System - Find Rating

I'm trying to rate Types of Fruit by the best possible rating given by users. I have a Fruit table called Ratings just for submitting ratings with the following Information Fruit_id int From_ID int Rating int now I'm trying to run a SQL command like the following select From_ID, AVG(Rating) AS Rating FROM Ratings Group BY `Fruit_ID`...

SQL Server 2005 replication - indicate data is replicated

Hi I am using SQL Server 2005 replication to replicate data from one table on a server to the a table on a remote server. This is happening in real time. i.e whenever the data is changed at the publisher, it is then replicated to the subscribers. From the publisher's server I need to know what data has been replicated to the subscrib...

Communicate Sql Server over http

Hi, I just wanted to have some suggestions here. Right now, I have a client server application, using a windows application (.net) as a client and SQL server 2005 as a database server. The application is working fine and they are using within their intranet network; but suddenly a change comes in and they are saying that they would l...

TSQL Reverse FOR XML Encoding

I am using FOR XML in a query to join multiple rows together, but the text contains quotes, "<", ">", etc. I need the actual character instead of the encoded value like """ etc. Any suggestions? ...

how to import flat file source to database using sql

hi. im currently want to inport my data from flat file to the database. the flat file is in a txt file. in that txt file, i save a list of URLs. example: http://www.mimi.com/Hotels-g303188-Rurrenabaque-Hotels.html im using the SQL Server Import and Export wizard to do it. but when the time of execution, it has error saying Error 0xc...