tsql

How can I make multiple records to be printed as a single row

This three columns are taken from 3 tables. In other words, these records are retrieved by joining 3 tables. It is basically a very simple time sheet that keeps track of shift starts time, lunch time and so on. I want these four records to show in one row, for example: setDate --- ShiftStarted --- LunchStarted --- LunchEnded ---- ...

simple Group By

Sorry for this simple question, but i'm missing the forest through the trees. These are my tables (i left the sparepart-table because i'm only looking for two special fk's): I need the distinct rows from tabData which are referenced in the child-table tabDataDetail with fiSparePart=8837 and 8969. The following gives me only that rows ...

How can I use optional parameters in a T-SQL stored procedure?

I am creating a stored procedure to do a search through a table. I have many different search fields, all of which are optional. Is there a way to create a stored procedure that will handle this? Let's say I have a table with four fields: ID, FirstName, LastName and Title. I could do something like this: CREATE PROCEDURE spDoSearch...

How to escape parenthesis in SQL query?

Hello, Using Microsoft SQL Server 2005 - wondering how can I escape parenthesis in my query? I'm trying to find the string position of "(" in one of my columns returned, but Management Studio keeps thinking I'm opening another command when I use that. I've tried \ but that isn't escaping it. Thank you! ...

checking for smalldatetime column equal to GetDate() - ignoring time

I have a column of smalldatetime type, date I'd like to run a query that only retrieves rows: where date = convert(smalldatetime,GetDate()) However, this is never finding matches as it is also comparing the times. Ie: 01-01-2010 12:23:00 != 01-01-2010 12:25:00 How can I find matches on only the date portion? ...

WCF and Stored Procedure Options

I'm making my first WCF Service and I am unsure which route I should take with stored procedures and Linq to Sql. I understand that I can drag and drop stored procs to my DBML file and call them that way, or call them directly, not using the dbml. Is there a reason why i should choose one over the other? I guess I'm a little confused... ...

Dynamic Function Issue

Hi All, I am trying to create some dynamic DDL to build a function and when I run it, it keeps giving me an error. I am not sure what I am doing wrong with the format....I have tried a lot of different things and now it is just out of curiousity that I want to understand how to get it to work. Any input is greatly appreciated. CODE: ...

TSQL select last value by week?

I have the following query: SELECT row_ID, value, date_added FROM dbo.Report WHERE salesman_ID = @salesman_ID AND row_ID IN ( SELECT MAX(row_ID) FROM dbo.Report WHERE salesman_ID = @salesman_ID GROUP BY ( date_added ) ) ORDER BY d...

sql query converting rows to columns

I have 2 tables Order Table - OrderId- Primary key Order Details Table - OrderID foreign key from order table. Orderdetail table has the information for all the products which were ordered in the specific order For example Order Order Detail Table ID CustomerID OrderDate ID ...

SQL Only get numbers after decimal

Example 2.938 = 938 Thanks ...

Split concatenated field into separate parts

Using SQL Server 2008, I am trying to do something similar to this post regarding splitting a concatenated field into separate parts for normalization purposes. The problem with the solutions in the linked post is that they only work for delimited values. The data I am attempting to split would be similar to: UserID -- ConcatField 1 -...

Alternative to DISTINCT Function

Is there a better way to get all distinct values from three columns in one table other than using the DISTINCT function? I've also tried GROUP BY, but there doesn't seem to be any noticeable difference in the cost. SELECT DISTINCT Table1.Col1, Table2.Col1, Table1.Col3 FROM Table1 INNER JOIN Table2 ON Table1.FK = Table2.ID WHERE Table1....

Stop execution when ROLLBACK an SQL transaction

Is there a way to stop executing code once I ROLLBACK a transaction in T-SQL? For example, in the code below I want 'Message 1' to print, but not 'Message 2'. BEGIN TRANSACTION GO PRINT 'Message 1' ROLLBACK TRANSACTION GO PRINT 'Message 2' GO ...

Problem getting the progress status of a SQL-Server restore job

I want to use the script from http://www.wisesoft.co.uk/articles/tsql_backup_restore_progress.aspx to get progress information of a specific running SQL-Server restore job. To achive this, I have changed it a little bit: SELECT command, s.text, start_time, percent_complete, CAST(((DATEDI...

TSQL syntax issue with LTRIM(RTRIM not working correctly

What is wrong with this statement that it is still giving me spaces after the field. This makes me think that the syntax combining the when statements is off. My boss wants them combined in one statement can someone help me understand what I am doing wrong? Case WHEN LTRIM(RTRIM(cSHortName))= '' Then NULL WHEN cShortname is NOT NU...

How to use OPENXML to load XML data into existing SQL Table?

I am a newbie to OPENXML. But I am trying to load a .XML file into a SQL table that I created for this. I do not receive any errors with this code, but it doesn't insert any records either. This is the table I created in 2008 SQL Server: CREATE TABLE HOMEROOM( HOMEROOM_TEACHER INT, HOMEROOM_NUMBER INT, ENTITY_ID INT) And this is th...

Copy substring of one column and copy to another.

This is a bit above my level. But am trying to learn. I don't want to seem like I'm just trying to get my homework done but would appreciate any help pointers. I am trying to find a substring (postcode) in an address column and once found, copy to the post code column I have the following sql which finds columns that match a postcode pa...

Between .. and clause in sql server?

hi, in my web application i am displaying videos of user, I want to display the videos which are between 30 days only. Videos that are uploaded last 30 days only. i write query like this but it is not working fine.. select * from videos where posteddate between getdate()-30 and getdate() order by posteddate desc can u he...

How to sort string in sql server

How to sort this data in sql server as Pre-OP 1, Pre-Op 2 like wise Pre-OP 1 Pre-OP 10 Pre-OP 11 Pre-OP 12 Pre-OP 13 Pre-OP 14 Pre-OP 15 Pre-OP 16 Pre-OP 17 Pre-OP 18 Pre-OP 19 Pre-OP 2 Pre-OP 20 Pre-OP 21 Pre-OP 22 Pre-OP 23 Pre-OP 24 Pre-OP 25 Pre-OP 26 Pre-OP 27 Pre-OP 28 Pre-OP 29 Pre-OP 3 Pre-OP 30 Pre-OP 4 Pre-OP 5 Pre-OP 6 Pre-OP...

T-SQL Foreach Loop

Scenario I have a stored procedure written in T-Sql using SQL Server 2005. "SEL_ValuesByAssetName" It accepts a unique string "AssetName". It returns a table of values. Question Instead of calling the stored procedure multiple times and having to make a database call everytime I do this, I want to create another stored procedure th...