tsql

What's the best way to parse an Address field using t-sql or SSIS?

I have a data set that I import into a SQL table every night. One field is 'Address_3' and contains the City, State, Zip and Country fields. However, this data isn't standardized. How can I best parse the data that is currently going into 1 field into individual fields. Here are some examples of the data I might receive: 'INDIANAPOL...

SQL Distinct keyword in assignment statement

I have a query that works: DECLARE @ProductID int SET @ProductID = '1234' SELECT DISTINCT TOP 12 a.ProductID FROM A a WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE b.ProductID = @ProductID) AND a.ProductID != @ProductID It returns a list of 12 product numbers, all unique. I need to store these results in a variable, comm...

Find amount of updated rows in T-SQL

I need to find the amount of updated rows UPDATE Table SET value=2 WHERE value2=1 declare @aaa int set @aaa = @@ROWCOUNT It doesn't work. How can I do that? ...

Auto Generated - CREATE Table Script in SQL 2008 throws error

I scripted the tables in my dev database using SQL 2008 - generate scripts option (Datbase->right click->Tasks->Generate Scripts) and ran it on the staging database, but the script throws the below error for each table Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('. Msg 319, Level 15, State 1, Line 15 Incorrect sy...

How to SUM columns on multiple conditions in a GROUP BY

I am trying to return a list of Accounts with their Balances, Outcome and Income Account Transaction ------- ----------- AccountID TransactionID BankName AccountID Locale Amount Status Here is what I currently have. Could someone explain where I am going wrong? select a.ACCOUNT...

Tsql ordering data by specific string values

Hi every body I have an audit table that have data for insert, update and delete operations. I am writing a report that will display data in the order of Insert, Update and Delete. I don't think the order by clause will help. any help? ...

Which command would replace IDENTITY INSERT ON/OFF from SQLServer in Oracle?

Hello, I have to migrate this query (simplified here) from T-SQL to ORACLE SET IDENTITY_INSERT table ON INSERT INTO table (id, value) VALUES (1, 2) SET IDENTITY_INSERT table OFF id being an Identity field in SQLServer. I have the same table with a sequence in ORACLE, I couldn't find a snippet that shows how to disable the sequence ...

is it possible to select EXISTS directly as a bit?

I was wondering if it's possible to do something like this (which doesn't work): select cast( (exists(select * from theTable where theColumn like 'theValue%') as bit) Seems like it should be doable, but lots of things that should work in SQL don't ;) I've seen workarounds for this (SELECT 1 where... Exists...) but it seems like I shou...

What short query will return an vector (a one-column column table) representing a set of integers from x1 to x2?

Assuming we have no actual table to select the data from, what do we need to SELECT to return a table consisting of one integer column containing all integer numbers starting with x1 and finishing with x2 and sorted in ascending order? ...

Replace always replacing null values

Why does left(FIELD, replace(nullif(charindex('-', FIELD), 0), null, len(FIELD))) always return null? The idea behind the query is that if charindex() returns 0, then convert the results into null, then convert the null into the length of the field. So if '-' does not exist, show the whole string. For some reason it makes every row eq...

How do I close SQL Server output subwindow which appears when I run a T-SQL script from inside Visual Studio 2010?

How do I close SQL Server output subwindow which appears when I run a T-SQL script from inside Visual Studio 2010? It neither has nay kind of control buttons like [x] or[-=] visible, nor any other obvious means of closing. ...

Test Column exists, Add Column, and Update Column

I'm trying to write a SQL Server database update script. I want to test for the existence of a column in a table, then if it doesn't exist add the column with a default value, and finally update that column based on the current value of a different column in the same table. I want this script to be runnable multiple times, the first time...

Sqldatareader and rowcount

Hi, I have a query: declare @Code nvarchar(100) select @Code="BMW" select name from NewCars where code=@Code if @@rowcount = 0 Select name from OldCars where code=@Code In Sql managment studio first part give me 0 resuklts, and second 1 one result, and that is ok, but in sqldatareader I use the same query ofcource without: declare @...

problem adding a where clause to a T-sql LEFT OUTER JOIN query

SELECT TOP (100) PERCENT dbo.EmployeeInfo.id, MIN(dbo.EmployeeInfo.EmpNo) AS EmpNo, SUM(dbo.LeaveApplications.DaysAuthorised) AS DaysTaken FROM dbo.EmployeeInfo LEFT OUTER JOIN dbo.LeaveApplications ON dbo.EmployeeInfo.id = dbo.LeaveApplications.EmployeeID WHERE (YEAR(dbo.LeaveApplications.ApplicationDate) = YEA...

SQL Server schema-owner permissions

if i do: CREATE SCHEMA [test] AUTHORIZATION [testuser] testuser doesn't seem to have any permissions on the schema, is this correct? I thought as the principal that owns the schema, you had full control over it? What permission do i need to grant testuser so that it has full control over the test schema only? Edit: by "full control"...

Finding Shared Columns in SQL Server

Is there a quick query I can run that returns all columns of the same name between two tables? I have many pairs of tables that I know are tied together, but I'm not sure which of the 50 or so columns they have in common. ...

Table Variables in SSIS

In one SQL Task can I create a table variable DELCARE @TableVar TABLE (...) Then in another SQL Task or DataSource destination and select or insert into the table variable? The other option I have considered is using a Temp Table. CREATE TABLE #TempTable (...) I would prefer to use Table Variable so that it remains in memory. But ...

What are the advantages of a query using a derived table(s) over a query not using them?

I know how derived tables are used, but I still can’t really see any real advantages of using them. For example, in the following article http://techahead.wordpress.com/2007/10/01/sql-derived-tables/ the author tried to show benefits of a query using derived table over a query without one with an example, where we want to generate a re...

Is derived table executed once or three times?

Every time you make use of a derived table, that query is going to be executed. When using a CTE, that result set is pulled back once and only once within a single query. Does the quote suggest that the following query will cause derived table to be executed three times ( once for each aggregate function’s call ): SELECT AVG...

Creating an index on a view with OpenQuery

SQL Server doesn't allow creating an view with schema binding where the view query uses OpenQuery as shown below. Is there a way or a work-around to create an index on such a view? ...