sql

Selecting most recent date between two columns

If I have a table that (among other columns) has two DATETIME columns, how would I select the most recent date from those two columns. Example: ID Date1 Date2 1 1/1/2008 2/1/2008 2 2/1/2008 1/1/2008 3 1/10/2008 1/10/2008 If I wanted my results to look like ID MostRecentDate 1 2/1/2008 2 ...

Limiting a date range with exactness in MS SQL / SQL Server 2005

I want to limit a report to return records from Date A through Date B. This is what I have been doing: declare @startDate varchar(20) declare @endDate varchar(20) set @startDate = '01/01/2008' set @endDate = '04/01/2008' -- test what are the start and end dates select min(date),max(date) from view_Inspections where date between @startD...

Why does the SqlServer optimizer get so confused with parameters?

I know this has something to do with parameter sniffing, but I'm just perplexed at how something like the following example is even possible with a piece of technology that does so many complex things well. Many of us have run into stored procedures that intermittently run several of orders of magnitude slower than usual, and then if yo...

Are there Free SQL Server Mgmt Studio Autocomplete Add-ins?

I know Red Gate has such a product, but I'd rather not have to pay for something which, in my opinion, should be built into SSMS. Thanks! ...

SQL - CASE expression inside WHERE

I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm trying to use this to filter results from my select statement, based on a contract number which will be passed in by the user's application. My code currently throws an error of 'Invalid param...

Is there a way to express a Postgres literal int in hexadecimal?

I have a big list of hexadecimal numbers I'd like to insert into a PostgresQL table. I tried something like this: INSERT INTO foo (i) VALUES (0x1234); ...but that didn't work. Is this possible? ...

What is the equivalent of varchar(max) in Oracle?

What is the equivalent of varchar(max) in Oracle? CLOB? ...

What's the best way to copy a subset of a table's rows from one database to another in Postgres?

I've got a production DB with, say, ten million rows. I'd like to extract the 10,000 or so rows from the past hour off of production and copy them to my local box. How do I do that? Let's say the query is: SELECT * FROM mytable WHERE date > '2009-01-05 12:00:00'; How do I take the output, export it to some sort of dump file, and then...

Managing date formats differences between PHP and MySQL

I'm writing my first PHP app that has to directly deal with dates, and thus to directly deal with the fact that PHP and MySQL have different date formats. My question is: what's the most elegant way to manage this difference? I have the following two functions to manage the difference using php: function mysql_date($php_date) { r...

a question about sql group by

I have a table named visiting that looks like this: id | visitor_id | visit_time ------------------------------------- 1 | 1 | 2009-01-06 08:45:02 2 | 1 | 2009-01-06 08:58:11 3 | 1 | 2009-01-06 09:08:23 4 | 1 | 2009-01-06 21:55:23 5 | 1 | 2009-01-06 22:03:35 I want to work out a sql...

How to set a maximum execution time for a mysql query ?

I would like to set a maximum execution time for sql queries like set_time_limit() in php. How can I do ? ...

SQL code import into Access 2007

Hey everyone, I basically need to know how to import SQL code into Access. I've tried one way but that requires me to do one table and one value at a time which takes a lot of time. Can anyone help? ...

Why does SQL force me to repeat all non-aggregated fields from my SELECT clause in my GROUP BY clause?

This has bugged me for a long time. 99% of the time, the GROUP BY clause is an exact copy of the SELECT clause, minus the aggregate functions (MAX, SUM, etc.). This breaks the Don't Repeat Yourself principle. Is there a good reason for this? edit I realise that some implementations allow you to have different fields in the GROU...

ASP.NET MVC TDD with LINQ and SQL database

I am trying to start a new MVC project with tests and I thought the best way to go would have 2 databases. 1 for testing against and 1 for when I run the app and use it (also test really as it's not production yet). For the test database I was thinking of putting create table scripts and fill data scripts within the test setup method an...

Problem with SQL Join

I have two tables, tblEntities and tblScheduling. tblEntities: EntityID ShortName Active 1 Dirtville 1 2 Goldtown 1 3 Blackston 0 4 Cornfelt 1 5 Vick 1 tblScheduling: ScheduleID EntityID SchedulingYearID 1 1 20 2 ...

Join and Include in Entity Framework

I have the following query in linq to entities. The problem is that i doesn't seems to load the "Tags" relation even thou i have a include thingy for it. It works fine if i do not join on tags but i need to do that. var items = from i in db.Items.Include("Tags") from t in i.Tags ...

Insert Picture into SQL Server 2005 Image Field using only SQL

Using Ms SQL Server 2005 and Management Studio how do I insert a picture into an Image type field of a table? Most importantly how do I verify if it is there? ...

SQL Reporting Services: Why does my report shrink when it's emailed?

I created a simple report and uploaded it to my report server. It looks correct on the report server, but when I set up an email subscription, the report is much narrower than it is supposed to be. Here is what the report looks like in the designer. It looks similar when I view it on the report server: [http://img58.imageshack.us/img58/...

Setting up Dim and Fact tables for a Data Warehouse

I'm tasked with creating a datawarehouse for a client. The tables involved don't really follow the traditional examples out there (product/orders), so I need some help getting started. The client is essentially a processing center for cases (similar to a legal case). Each day, new cases are entered into the DB under the "cases" table....

How do I make a boolean calculated field in TSQL and join on that calculated field?

First check out this code. I seems like it should work for me, but it doesn't! (surprise!) Anyway, this is what I tried first: SELECT Status as status, Address as ip, PCName as pc_name, (Numbers.Phone = 'CPU/' + PCName) as cpu_contact, (Numbers.Phone = 'PC/' + PCName) as pc_contact, (Numbers.Phone = 'LOGIN/' + PCName) as login_contac...