tsql

Database: Does the appendix 'HX' have significant value?

I'm working with a legacy application with surprise surprise, next to no useful documentation on naming convensions or over all data structure. There are similarly named tables in the database with one of them ending in HX. Does this hold any significance in anyones database experience? The data seems to be replicated so I would assume...

sql server: will this datetime work?

SELECT "2 0 Analytical (Testing Phase)" FROM "dbo"."Lab Occurrence Form" WHERE (("Occurrence Date" BETWEEN @P1 AND @P2 ) AND ("2 0 Analytical (Testing Phase)" LIKE ''%2.%'' ) ) ', N'@P1 datetime,@P2 datetime','2010-04-30 00:00:00','2010-04-01 00:00:00' is this the correct format to return records between a certain datetime?...

Create Unique Field Value by Concatenation

How can you generate a unique value for a field that matches a concatenation of certain fields and a random number i.e. First Name: Jim Last Name: Jones Field Value: jimjones0345 obviously there's a need to ensure that this value was not populated before. How would one go about this? ...

SQL Select Master Records and Display Number of Detail Records for Each

I have a Master and Detail table, the Detail linking to the Master record on a FK reference. I need to display all the data from the Master table, and the corresponding number of details for each record, i.e. MASTER TABLE ID Name Age 1 John 15 2 Jane 14 3 Joe 15 DETAIL MasterID Subjects 1 Trigonometry 1 Chemistr...

SQL Server: Computed Expression error

Here's a table with a computed column in SQL Server 2005: CREATE TABLE footable AS [identifier] nvarchar(255) NOT NULL, [result] AS CASE WHEN [identifier] like '%\[%]' escape '\' THEN LEFT( [identifier], CHARINDEX('[',[identifier]) - 1 ) END Here's what I'm expecting: identifier result ======================...

Select non duplicate values from rows that are less than a specified number of minutes apart

I have a huge table with the following format: DATETIME NUMBER -------------------------------------- 2009-03-31 16:05:52.000 2453651622 2009-03-31 16:16:12.000 30206080 2009-03-31 16:16:16.000 16890039 2009-03-31 16:16:28.000 2452039696 2009-03-31 16:16:33.000 140851934 2009-03-31 16:16:51.000 245312...

90 days range using SQL server

Hi, is there a way in SQL server that i can count the items 90 days prior to the inception date and the items 90 days after the inception date. Example: select site, count(*) from mytable where date >=10/1/2009' and date <'12/30/2009' group by site 90 days before - after inception date. prior to inception date = 7/3/2009. inceptio...

sp_xml_preparedocument error handling, what does it mean: SELECT @err = @@error + coalesce(@err, 4711)

Here (and not only there) people are using the following code to handle an error from sp_xml_preparedocument EXEC @err = sp_xml_preparedocument @hdoc OUTPUT, @xmlDoc SELECT @err = @@error + coalesce(@err, 4711) Are there any reasons for this? And what does 4711 mean in this context? ...

Dealing with periods and dates without using cursors

I would like to solve this issue avoiding to use cursors (FETCH). Here comes the problem... 1st Table/quantity ------------------ periodid periodstart periodend quantity 1 2010/10/01 2010/10/15 5 2st Table/sold items ----------------------- periodid periodstart periodend solditems 14343 2010/10/05 2010/10/06 2 Now...

Group by problem

I need to group by ,even if is there a difference in values for a column by converting to a text value and needs that text value to be included in select list How is this possibe? SELECT col1,col2 FROM ( SELECT col1,col2 FROM table1 UNION ALL SELECT col1,col2 FROM table2 UNION ALL SE...

Need to find first empty field, and update

Hello, I am working with a database schema that I cannot change - and I need to run a bulk update. I am moving data between source and target databases, with the same schema. I know this is awful, but it is what I am working with right now. The schema is as follows: Car CarType1 CarType2 CarType3 CarType4 What I am trying to do - is...

How to get SQL Server schema changes especially "column name" changes

I am using trigger to trace DDL changes but when I change column name from the SQL Server Management Studio the trigger is not working? create TRIGGER trgLogDDLEvent ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS FOR AS DECLARE @data XML SET @data = EVENTDATA() IF @data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)') <> 'CREATE_ST...

SQL Server 2008 Transaction, rollback required?

Hi, I have a stored procedure that has a BEGIN TRANSACTION and COMMIT TRANSACTION statement. Within the transaction is a select query WITH(XLOCK, ROWLOCK). The transaction can potentially fail due to some calculations that cause an arithmetic overflow error if out of bounds values are supplied. This error would happen before any inser...

How to only add values that end with 's'?

I'd like to do this but I know this isn't the right syntax: INSERT INTO TBL1 SELECT Col1 FROM TBL2 WHERE Col1.endswith('s') ...

Get value inserted in RowGuid?

Hi, i have a simple table "MyTable" with single colum "id" which type is uniqueidetifier and rowguid is set to true. I insert new values like this INSERT INTO MyTable DEFAULT VALUES how to get inserted by server guid ? Best Regards, Iordan ...

SQL Server - pull X random records per state

I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? ...

Dynamic Variable Names in SQL

I have a (hopefully) quick SQL question that is driving me nuts and I have not been able to locate an answer anywhere. I have the following SQL trigger: DECLARE @ABCOwnerGroup varchar(30) DECLARE @DEFOwnerGroup varchar(30) SET @ABCOwnerGroup='GROUP ONE' SET @DEFOwnerGroup='GROUP TWO' etc.. --IF OWNERGROUP IS MISSING (Location NOT NULL...

TSQL - Locating first occurrence of value in a string

I need to run a simple select statement for a column called AddrZip to show all records that contain '1/2 ' after the first space in the column. In Access 2007 it would be: **Left([Names],InStr(1,[Names]," ")-1), but can't find out how to do it in SQL 2005. All help will be appreciated. ...

Display data by claimnubers on grid view page

Hello, I am using C#. net and SQL server 2005. I am uploading files(word, PDF) to DB and displaying on page using grid view. I have different claim Numbers. like co50000006( like 10 rows). I mean the claim number has different files. I have other claim numbers on my table.(like c08000131, c01000001). I want to display only one claim n...

Any issues about using NEWID() for API key?

I am working on a small piece that will generate an API Key using SQL Sever's NEWID(). The key will be used to access certain parts of our web app and will be passed in through a URL. The key is generated when a new API consumer is created (in a Stored Proc). The key is unique for each consumer. While the initial number of consumers is e...