sql

Easy way to force DATEPART to output as fixed-length?

When I do for example DATEPART(mm, GETDATE()) I get the result of the month being "8" for August. If i did the same thing in December I would get "12". Those are two different length results. Is there a way to get the DATEPART results to always be a fixed length? So that for example the months would show up as 08 or 12. And days would b...

What DB for big databases?

I'm embarking on a project which will likely cross several million rows in the short future, so I am researching the database I use as that is sure to prove an issue. From what I have read, SQL in all its incarnations has issues once you get to the 2,000,000 rows issue for a table. Is there a good database recommended for these big size ...

Django query for large number of relationships

I have Django models setup in the following manner: model A has a one-to-many relationship to model B each record in A has between 3,000 to 15,000 records in B What is the best way to construct a query that will retrieve the newest (greatest pk) record in B that corresponds to a record in A for each record in A? Is this something tha...

[sp_executesql] Incorrect syntax near 'sp_executesql'

I don't understand why the following is giving me the error. I thought it was related to the commented out section, but @SQL is nvarchar(4000). BEGIN sp_executesql N'SELECT ''td''' --sp_executesql @SQL, N'@StartDate DateTime, @EndDate DateTime, @End2 DateTime, @Program varchar(4)', @StartDate, @EndDate, @End2, @Program END ...

SQL Server 2005 Profiler How do you view the entire Stored Procedure Chain

SQL Server 2005 Profiler shows that a Stored Procedure (SP) was called and what variables were passed. However I don't know how to get profiler to show me subsequent calls. By this I mean when SP A calls SP B and SP B calls SP C. Is there a way to get profiler to show this chain? Thanks ...

join two tables to get tomorrow's price (and the price two days from "now")

I'm trying to do a JOIN query to analyze some stocks. In my first table called top10perday, I list 10 stocks per day that I have chosen to "buy" the next day and sell the following day: date symbol 07-Aug-08 PM 07-Aug-08 HNZ 07-Aug-08 KFT 07-Aug-08 MET ... 08-Aug-08 WYE 08-Aug-08 XOM 08-Aug-08 SGP 08-Aug-08 JNJ ...

How long should SQL email fields be?

Hi I recognize that an email address can basically be indefinitely long so any size I impose on my varchar email address field is going to be arbitrary. However, I was wondering what the "standard" is? How long do you guys make it? (same question for Name field...) Thanks, Mala update: Apparently the max length for an email address is...

Using a custom field in WHERE clause of SQL query.

My question is very similar to this one How to reference a custom field in SQL I have the following query: SELECT * , (SELECT COUNT( id ) FROM cms_store_items WHERE speaker = cms_store_items_speakers.id ) AS count FROM cms_store_items_speakers LIMIT 0 , 30 I need to add a WHERE clause that looks like WHERE count > 0 but when I do I g...

Oracle SQL: What is the point of this DECODE statement?

I'm working on maintaining some Oracle sql statements written by someone else, and all over the place I keep seeing this same pattern repeated in lots of statements and pl/sql blocks: select DECODE(NVL(t1.some_column,'~'),'~',t2.some_column,t1.some_column) some_column from t1, t2 where ... Now, isn't this identical to this much simple...

MS SQL Server stored procedure error 'incorrect syntax'

Hi, This is my stored procedure: ALTER PROCEDURE its.sp_WriteTransaction ( @LoginID int, @PersonID int, @BusinessID int, @TransType smallint, @LastHost varchar(15), @TransData varchar(255) ) AS DECLARE @TransDate DATETIME SET @TransDate = GETDATE() INSERT INTO Transactions (LoginID, PersonID, BusinessID, TransDate, T...

When Full Text Searching an XML column can you use XML Querying?

I am working on a database that is very localized, for example sake: Tables: Product - Has non localized columns (item number, created, etc) Product_Local - Has localized columns (description) and a language identifier column I am using SQL 2008 and full text searching requires for its word breakers that I set the language per column. ...

Finding missing sequence in a table

Hello all. I'm using the Oracle 10g Database. i'm trying to figure out how to write a simple sql query to: find the missing numbers in a table between say 86002895 and 86005197 (inclusive), There are 1955 rows between 86002895 and 86005197. Ex: Current Scenario : table_1 : tracking_no | id_value 86002895 | 10 86002896 | 10 86002899 |...

need a sql query to wrap text coming in differnt colums to one column

i am running the following query SELECT ora_database_name AS SERVER_NAME,'CNTODSSTG' AS DB_NAME,p.owner,p.object_name,s.text FROM all_procedures p,all_source s WHERE p.owner<>'SYS' AND p.owner<>'SYSTEM' AND s.TYPE='PROCEDURE' AND p.object_name=s.name GROUP BY p.owner,p.object_name,s.text s.text is the coding inside the procedure. Bu...

How can I list ALL grants a user received?

I need to see all grants on an Oracle DB. I used the TOAD feature to compare schemas but it does not shows temptable grants etc. so there's my question: How can I list all grants on a Oracle DB? ...

How to Access Cursor Columns Without FETCH .. INTO

I'm using SQL Server to build stored procedures, and I'm using cursors to loop through a select statement I'm defining the cursor as follow: DECLARE @c_col1 varchar(max); DECLARE @c_col2 varchar(max); DECLARE c as CURSOR FOR SELECT col1, col2 FROM table; OPEN c; FETCH NEXT FROM c INTO @c_col1, @c_col2; SELECT @c_col1, @c_col2; I...

sys.servers - what table was in SQL 2000

Hello, In SQL Server 2005 in the sys.servers information about mapped remote SQL Server instance is stored. How were remote servers mapped in SQL Server 2000? BR, Adam ...

How to make SQL Server 2008 Check Constraint of a table Allow Only Certain characters?

I'd like to create a check constraint for a table in SQL 2008 which would allow A-Z characters (non case sensitive), numbers, hyphen (-), dot(.), space and underscore (_). Below is my current expression: ([company_code] not like '%[^A-Za-z0-9_ .+]%'). It fulfills all the above requirements except for hyphen. How could I make the express...

Fluent nHibernate: How to map 2 tables with no primary keys defined

I have a legacy system where the relationship between 2 tables haven't been defined explictly and there aren't any keys (primary or unqiue defined). The only related columns is 'Username' Something like: Table: Customer Column: Id, Column: Username, Column: FirstName, Table: Customer_NEW Column: Username Column: FirstNameNew I have ...

Find if a given date is in a list of date ranges

I have a list of events that need processing on a constant basis. The process takes care of the event for some time, whereupon it needs processing again. This means that I end up, for each event, with a list of date ranges in which the event was "taken care of" and any gaps mean the event was in an unprocessed state at that time. For thi...

find likewise data from two tables in mysql

i have two tables in my database one is A other one is B A is having few fields in which three are id,name,group B is having feilds like id,title,description, etc. i have to search the id's of title and description that are having data similar to table A's name or group and then have to insert the id's in a field of table A. For exampl...