tsql

SQL Server 2008 - Bulk Insert a whole text file into one field

I have a text file (txt) containing formatted text (just line breaks, carriage returns and tabs) It also contains German language characters. I want to use the Bulk Insert comment in T-SQL to read in the text file into one field within a database table. I ran this command: CREATE TABLE #MyTestTable ( MyData NVARCHAR(MAX) ) BUL...

T-SQL: Displaying data from multiple columns into one output column using CASE and COALESCE

I have the following SQL code that runs against a Change Request database. Each record has several columns that represent affected US regions. If the change affects a region the value will be 1 otherwise NULL. So I am adding the values in each column to determine if more than one region is affected, the answer will be greater than 1. ...

How can convert 15000000.00 to 15,000,000 in SQL server?

There is a one value 15000000.00 with Numeric datatype in SQL server. How can i get or convert the value 15000000.00 to 15,000,000? ...

CTE with dynamic query

I have a dynamic query which I call and I put the Result set in a variable table INSERT INTO @outTable EXEC sp_executesql @query; The problem is that @outTable should change When adding column in @query - @query is another stored procedure. I tried to use a CTE (WITH), but it does not work with EXEC. Is this possible do to in T-SQ...

Change string in SQL Server to abbreviate

How do I return the everything in a string from a sql query before a certain character? My data looks like this: HD TV HM45VM - HDTV widescreen television set with 45" lcd I want to limit or truncate the string to include everything before the dash. So the final result would be "HD TV HM45VM" ...

TSQL: grouping customer orders by week

I have a table with a collection of orders. The fields are: customerName (text) DateOfOrder (datetime). I would like to show totals of orders per week per customer. I would like to have it arranged for the Friday of each week so that it looks like this: all dates follow mm/dd/yyyy "bobs pizza", 3/5/2010, 10 "the phone co",3/5/2010...

How to bulk insert data from ref cursor to a temporary table in PL/SQL

Could anyone tell me how to bulk insert data from a ref cursor to a temporary table in PL/SQL? I have a procedure that one of its parameters stores a result set, this result set will be inserted to a temporary table in another stored procedure. This is my sample code. CREATE OR REPLACE PROCEDURE get_account_list ( type_id in account_t...

Can we write subquery in between SELECT and FROM

i want to know, how to write subquery in between SELECT and FROM as SELECT Col_Name,(Subquery) From Table_Name Where Some_condition ...

SqlDependencies - When using a Stored Procedure as command, how do I specify input parameters

When using the folllwing to create the command: _command = new SqlCommand(statementOrProcedure, _connection) { CommandType = CommandType.StoredProcedure }; and the stored procedure requires no input parameters, it works fine. As soon as the stored proc needs input parameters, I do the following: private void AddStoredProcedureParame...

T-SQL UPDATE trigger help

Hi iam trying to make an update trigger in my database. But i get this error every time the triggers trigs. Error MEssage: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(3rows) and heres my trigger ALTER TRIGGER [dbo].[x1pk_qp_update] ON [dbo].[x1pk] FOR UPDATE AS BEGIN TRY DE...

SQl to select from multiple tables

I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose status is 0 in emptable1 and from that list of empid i need to select empid whose week is 7 from the table emptable2 Please help :-) ...

SQL Join query, getting ManagerName

I have an tblEmployeeProfile & a tblPersonnel. tblPersonnel is an HR table, that consists of all employees in the company; tblEmployeeProfile contains details about an employee's position. tblPersonnel.PersonnelID tblPersonnel.FirstName tblPersonnel.MiddleName tblPersonnel.LastName tblPersonnel.PhoneNumber tblPersonnel.Email tblEmploye...

How to make awkward pivot of sql table in SQL Server 2005?

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 list of given labels can vary and it is possible that a new label name can come into at any given time. Given ...

Stored Proc : Use Return or Select at end?

If I'm creating a C# wrapper for a stored proc, and that sp only returns/selects (not sure) 1 value, should I use return or select at the end of that sp? This is for t-sql. ...

Make a final call to the Database when user leaves website (ASPX)?

I have a system set up to lock certain content in a database table so only one user can edit that content at a time. Easy enough and that part is working fine. But now I'm at a road block of how to send a request to "unlock" the content. I have the stored procedure to unlock the content, but how/where would I call it when the user just c...

Profiling statements inside a User-Defined Function

I'm trying to use SQL Server Profiler (2005) to track down some application performance problems. One of the calls being made is to a table-valued user-defined function. This function wraps a select that joins several tables together. In SQL Server Profiler, the call to the UDF is logged. However, the select that underlies the UDF isn't...

How do I insert into a unique key into a table?

I want to insert data into a table where I don't know the next unique key that I need. I'm not sure how to format my INSERT query so that the value of the Key field is 1 greater than the maximum value for the key in the table. I know this is a hack, but I'm just running a quick test against a database and need to make sure I always sen...

Is it a Good Practice to Add two Conditions when using a JOIN keyword?

I'd like to know if having to conditionals when using a JOIN keyword is a good practice. I'm trying to filter this resultset by date but I'm unable to get all the branches listed even if there's no expense or income for a date using a WHERE clause. Is there a better way of doing this, if so how? SELECT Branches.Name ,SUM(Expenses....

SQL Server: SELECT rows with MAX(Column A), MAX(Column B), DISTINCT by related columns

Scenario: Table A MasterID, Added Date, Added By, Updated Date, Updated By, 1, 1/1/2010, 'Fred', null, null 2, 1/2/2010, 'Barney', 'Mr. Slate', 1/7/2010 3, 1/3/2010, 'Noname', null, null Table B MasterID, Added Date, Added By, Updated Date, Updated By, 1, 1/3/2010, 'Wilma', 'The Great Kazoo', 1/5/2010 2, 1/4/2010, 'Betty', 'Dino', 1/...

Validate a string in a table in SQL Server - CLR function or T-SQL (Question updated)

I need to check If a column value (string) in SQL server table starts with a small letter and can only contain '_', '-', numbers and alphabets. I know I can use a SQL server CLR function for that. However, I am trying to implement that validation using a scalar UDF and could make very little here...I can use 'NOT LIKE', but I am not sure...