tsql

Tentative date casting in tsql

I am looking for something like TRYCAST in TSQL or an equivalent method / hack. In my case I am extracting some date data from an xml column. The following query throws "Arithmetic overflow error converting expression to data type datetime." if the piece of data found in the xml cannot be converted to datetime (in this specific case, ...

Why is doing a top(1) on an indexed column in SQL Server slow?

I'm puzzled by the following. I have a DB with around 10 million rows, and (among other indices) on 1 column (campaignid_int) is an index. Now I have 700k rows where the campaignid is indeed 3835 For all these rows, the connectionid is the same. I just want to find out this connectionid. use messaging_db; SELECT TOP (1) connect...

Date ranges intersections..

MS Sql 2008: I have 3 tables: meters, transformers (Ti) and voltage transformers (Tu) ParentId MeterId BegDate EndDate 10 100 '20050101' '20060101' ParentId TiId BegDate EndDate 10 210 '20050201' '20050501' 10 220 '20050801' '20051001' ParentId TuId BegDate EndDat...

Refreshing metadata on user functions t-SQL

I am doing some T-SQL programming and I have some Views defines on my database. The data model is still changing these days and I have some table functions defined. Sometimes i deliberately use select * from MYVIEW in such a table function to return all columns. If the view changes (or table) the function crashes and I need to recomp...

Replace duplicate spaces with single space in TSQL

I need to ensure that a given field does not have more than one space (not concerned about all white space, just space) between characters. So 'single spaces only' Needs to turn into 'single spaces only' The below will not work select replace('single spaces only',' ',' ') as it would result in 'single spaces onl...

Why won't this SQL CAST work?

I have a nvarchar(50) column in a SQL Server 2000 table defined as follows: TaskID nvarchar(50) NULL I need to populate this column with random SQL GUID's using the NEWID() function (I am unable to change the column type to uniqueidentifier). I tried this: UPDATE TaskData SET TaskID = CAST(NEWID() AS nvarchar) but I got the follow...

Faster way to transfer table data from linked server

After much fiddling, I've managed to install the right ODBC driver and have successfully created a linked server on SQL Server 2008, by which I can access my PostgreSQL db from SQL server. I'm copying all of the data from some of the tables in the PgSQL DB into SQL Server using merge statements that take the following form: with mbRem...

how Do I list all table names in SQL Server using T-SQL?

SELECT name FROM sys.databases -- this can list all database name in the server user database SELECT * FROM INFORMATION_SCHEMA.TABLES -- these two line can list the table for one particular database But how can I output the results like below? Database Table --------- ------------- db1 ...

Algorithms to trim leading zeroes from a SQL field?

I just came across the interesting problem of trying to trim the leading zeroes from a non-numeric field in SQL. (Since it can contain characters, it can't just be converted to a number and then back.) This is what we ended up using: SELECT REPLACE(LTRIM(REPLACE(fieldWithLeadingZeroes,'0',' ')),' ','0') It replaces the zeroes with sp...

Querying to construct XML in TSQL without a source table, only variables?

Using a SELECT statement in MS SQL 2005 TSQL, and no source table, just variables as listed below... @FirstName varchar(20) @LastName varchar(20) @ZipCode varchar(5) ...what syntax will build XML resembling the following? <XMLDATA><REC FirstName="JOHN" LastName="SMITH" ZipCode="98052" /></XMLDATA> ...

How do I perform an accent insensitive compare (e with è, é, ê and ë) in SQL Server?

I'm looking to compare two varchars in SQL, one would be something like Cafe and the other Café is there a way in SQL that will allow the two values to be compared. For instance: SELECT * FROM Venue WHERE Name Like '%cafe%' So if there is a venue with the name Big Bobs Café Extraordinaire it would be included in the result set? ...

How to join dynamic sql statement in variable with normal statement

I have a quite complicated query which will by built up dynamically and is saved in a variable. As second part i have another normal query and i'd like to make an inner join between these both. To make it a little more easier here is a little example to illustrate my problem. For this little example i used the AdventureWorks database. ...

Concatenation of fields in different rows

I'm stuck on an aggregation problem that I can't get to the bottom of. I have some data which is best summarized as follows id |phraseId|seqNum|word ========================= 1 |1 |1 |hello 2 |1 |2 |world 3 |2 |1 |black 4 |2 |2 |and 5 |2 |3 |white I'd like a query that gives back...

Will this force a reinitialize in Merge Replication Topology?

I need to add a couple of columns to a table that is a part of a replication set. It is not a constraint coulumn or a part of any article filters and it allows NULL. I have a pretty good idea that I can run this --> ALTER TABLE tblPlanDomain ADD ReportWageES VARCHAR (100) NULL and NOT force all my clients to reinitialize but I was hopin...

Insert multiple values using INSERT INTO (SQL Server 2005)

In SQL Server 2005, I'm trying to figure out why I'm not able to insert multiple fields into a table. The following query, which inserts one record, works fine: INSERT INTO [MyDB].[dbo].[MyTable] ([FieldID] ,[Description]) VALUES (1000,N'test') However, the following query, which specifies more t...

How to make additional columns on pivot of sql table in SQL Server 2005?

After getting such a great feedback from my last question. Maybe someone can help me with this problem also. I have to rotate a given table from an SQL Server but a normal pivot just doesn't work (as far as i tried). So has anybody an idea how to rotate the table into the desired format? Just to make the problem more complicated, the l...

SQL Server: Is it possible to prevent SQL Agent from failing a step on error?

I have a stored procedure that runs custom backups for around 60 SQL servers (mixes 2000 through 2008R2). Occasionally, due to issues outside of my control (backup device inaccessible, network error, etc.) an individual backup on one or two databases will fail. This causes this entire step to fail, which means any subsequent backup comm...

SQL Server Update with left join and group by having

I'm making an update to our datbase and would like to update rows that do not have existing items in another table. I can join the tables together, but am having trouble grouping the table to get a count of the number of rows UPDATE dpt SET dpt.active = 0 FROM DEPARTMENT dpt LEFT JOIN DOCUMENTS doc on dpt.ID = doc.DepartmentID GROUP ...

Put stored procedure result set into a table-- any shortcuts besides INSERT INTO...EXEC?

Hi everyone, I'm creating a temp table that's the result of a stored procedure's result set. Right now I'm using the create table statement, followed by insert into....exec. It gets the job done but I was curious if there are other ways of going about this? I wish it were possible to run a select into, with the stored procedure's res...

T SQL Count question

I'm making a report at work which looks at 5 columns which all contain v1aa, v1ab and v1ac. What I want to do is count up all the v1aa in the 5 columns and show the results (and the same for v1ab and v1ac) An example of how I like it to be displayed as is as follows :- Amber = 3 (v1aa code) Blue = 2 (v1ab code) Red = 1 (v1ac code) ...