tsql

SQL Query - Need some basic help

I'm in need of some basic TSQL help. Here's my table layout: Orders Table SSN ZipCode ZipLookup Table ZipCode State All columns are varchars. How would I get a list of States with the number of distinct SSN's in each state? Preferably, if a particular SSN has orders from multiple states only the state with the most orders wou...

to_date in SQL Server 2005

Does any one know how I would have to change the following to work with ms sql? WHERE registrationDate between to_date ('2003/01/01', 'yyyy/mm/dd') AND to_date ('2003/12/31', 'yyyy/mm/dd'); What I have read implies I would have to construct it using DATEPART() which could become very long winded. Especially when the goal would be to c...

sql rounding value

I have been successful at rounding my value to one decimal point, but I would like to trim off the multitude of trailing zeros. For example, this: ROUND(SUM(Duration),1) ...ends up giving me: 16.9000000000000000 how do I trim all those trailing zeros. mssql ...

How to Convert using of SQLite to Simple SQL command in C#

I want to get start with DayPilot control I do not use SQLite and this control documented based on SQLite. I want to use SQL instead of SQLite so if you can, please do this for me. main site with samples http://www.daypilot.org/calendar-tutorial.html The database contains a single table with the following structure CREATE TABLE...

SQL Server - Select one random record not showing duplicates

I have two tables, events and photos, which relate together via the 'Event_ID' column. I wish to select ONE random photo from each event and display them. How can I do this? I have the following which displays all the photos which are associated. How can I limit it to one per event? SELECT Photos.Photo_Id, Photos.Photo_Path, Photos.Ev...

SQL Get Top 10 records by date

I have a table full of bugs. The BugTitle is the page erroring and I also capture the error line. I would like to build an SQL Query that selects the top 10 bugs based on bugtitle and error line. I have this query: SELECT COUNT(BugTitle) AS BugCount, BugTitle, ErrLine FROM Bugs WHERE BugDate >= DateAdd(Day, -30, DateDiff(Day, 0, GetD...

How to configure reference to be deleted on parent table update?

I have two tables: info: ID, fee_id and fee: ID, amount and a reference between them (SQL Server 2008): ALTER TABLE info WITH CHECK ADD CONSTRAINT FK_info_fee FOREIGN KEY(fee_id) REFERENCES fee (ID) ALTER TABLE info CHECK CONSTRAINT FK_info_fee GO How to configure this reference that way so a record in fee will be deleted if inf...

SQL Get Top 10 bug records by count but with ID

I asked a question earlier today here. However, what I neglected to check with that question is how can I get AN id of those bug records? So for example, if I have a bug that has happened 3 times, how can I return one of the ID's from those 3 records? ...

Select proper columns from JOIN statement

I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns. SELECT * FROM table1 AS T1 INNER JOIN table2 AS T2 ON T1.ID = T2.ID I want to select all columns from table1 and only 1 column from table2. Is it possible to do that without enumerating all columns from table1 ? ...

Establishing Upper / Lower Bound in T-SQL Procedure

Hi. I am trying to establish upper / lower bound in my stored procedure below and am having some problems at the end (I am getting no results where, without the temp table inner join i get the expected results). I need some help where I am trying to join the columns in my temp table #PageIndexForUsers to the rest of my join statement...

Running Stored Procedure with parameters resulting from query

It's not hard to find developers who think cursors are gauche but I am wondering how to solve the following problem without one: Let's say I have a proc called uspStudentDelete that takes as a parameter @StudentID. uspStudentDelete applies a bunch of cascading soft delete logic, marking a flag on tables like "classes", "grades", and s...

UPDATE statement wrapped in an IF EXISTS block

I'm trying to write a DML script that updates a column but I wanted to make sure the column existed first so I wrapped it in a IF EXISTS block IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Client' AND COLUMN_NAME='IsClarityEnabled') BEGIN UPDATE Client SET IsClarityEnabled = 1 WHERE ClientID = 21 END So the...

Inline Conditional Statement in Stored Procedure

Here is the pseudo-code for my inline query in my code: select columnOne from myTable where columnOne = '#variableOne#' if len(variableTwo) gt 0 and columnTwo = '#variableTwo#' end I would like to move this into a stored procedure but am having trouble building the query correctly. I assume it would be something like select...

Is it safe to use Select Top and Delete Top in sequence?

I often write T-SQL loops that look like this in upgrade scripts: While Exists (Select * From #MyTable) Begin Declare @ID int, @Word nvarchar(max) Select Top 1 @ID=ID, @Word=[Word] From #MyTable -- Do something -- Delete #MyTable Where ID=@ID End Works a treat but I noticed the new Delete Top function which wou...

SQL Query - Get Sums from Two Different Levels of Hierarchy

A Project has multiple Tasks, which has multiple Assignments Projects (1-n) -> Tasks (1-n) -> Assignments A field on the Tasks table is Planned Hours. A field on the Assignments table is Assigned Hours. How do I get the Planned Hours and Assigned Hours for all Projects in a single query? ...

Problem convert column values from VARCHAR(n) to DECIMAL

I have a SQL Server 2000 database with a column of type VARCHAR(255). All the data is either NULL, or numeric data with up to two points of precision (e.g. '11.85'). I tried to run the following T-SQL query but received the error 'Error converting data type varchar to numeric' SELECT CAST([MyColumn] AS DECIMAL) FROM [MyTable]; I tried...

How to add additional rows to result set by condition

I have a table like this: ObjId Date Value 100 '20100401' 12 200 '20100501' 45 200 '20100401' 37 300 '20100501' 75 300 '20100401' 69 400 '20100401' 87 I have to add additional rows to result set for objId's, where there is no data at '20100501' **100 '20100501' null** 100 '20100401' 12 200 '20100501' 45 200 '20100401' 37 300 '2010050...

Assign a list of integers to an @var

I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int set @idOrder = (21319, 21320) for use in a series of statements where the 'WHERE' clause uses the IN operator delete Orders where idOrder in @idOrder instead of delete Orders where idOrder in (21319, 21320) ...

Append SQL table name with today's date

Hello all. I understand that I can change a sql table using the follow sp: EXEC sp_rename 'customers', 'custs' How would I go about appending this so that the new table has today's date as a suffix? I've attempt variations on the below theme with little success!! EXEC sp_rename 'customers', 'customers +(CONVERT(VARCHAR(8),GETDATE()...

Create a user-defined aggregate without SQL CLR

I am planning on deploying a database to SQL Azure, so I cannot use the SQL CLR. However, I have a need to create an aggregate function -- in my case, I need to STUnion a bunch of Geography objects together. (Azure is expected to support Spatial by June.) Is there another way to accomplish this, without making use of the CLR, in a query...