sql

Extract name and prename from email address?

Question: I have a table with the columns ID, Name, Prename, Mail Mail contains the e-mail address of a person, for example [email protected] Now I need to check whether Name='' or Prename='' and extract "John" from Mail and put it into Prename, and put "Doe" into column Name Can i do that with SQL, if yes how ? I use MS-SQL 2005 ...

Hibernate and dry-running HQL queries statically

I'd like to "dry-run" Hibernate HQL queries. That is I'd like to know what actual SQL queries Hibernate will execute from given HQL query without actually executing the HQL query against real database. I have access to hibernate mapping for tables, the HQL query string, the dialect for my database. I have also access to database if that...

SQL Server: Rethrow exception with the original exception number

I am using a TRY CATCH block in a stored procedure where I have two INSERT instructions. If something goes wrong, the CATCH block takes care of rolling back all changes made and it works fine, except one thing! The exception caught by my ASP.NET application is a SqlException with number 50000. This is not the original number! (the numb...

Union and order by

Consider a table like tbl_ranks -------------------------------- family_id | item_id | view_count -------------------------------- 1 10 101 1 11 112 1 13 109 2 21 101 2 22 112 2 23 109 3 30 101 3 31 ...

Sql-server Full Text CONTAINS + COLLATE to ignore accents issues

Hi all im struggling a little here with using COLLATE to ignore accents whilst also using Contains full text. Ive reduced the columns im searching down to just one for the example here, and im hard coding the actual parameter just to simply this until i understand it. If i have SELECT Col1, Title COLLATE SQL_Latin1_General_...

Sql Datareader null values

I am reading from a SQL datareader in C# and passing the values from the columns to a dropdownlist. There are two columns being read. Using IsDbNull, I am able to handle the null values. However, as I have the code written right now, if dr.GetString(0) is null, no values are passed along at all, while as long as only dr.GetString(1) (or ...

SQL Join vs Separate Query in Code without Join - Performance

I would like to know if there's a really performance gain between those two options : Option 1 : I do a SQL Query with a join to select all User and their Ranks. Option 2 : I do one SQL Query to select all User I fetch all user and do another SQL Query to get the Ranks of this User. In code, option two is easier to realize for...

Duplicate a row depending on some column value ?

Say i have a table 'users', with 'user_id', 'name' and 'multiplier'. If I do a basic select, I get one row for each user: SELECT * FROM users What I would like to do now, is to get mutiplier rows for each user. So if I have say two users in my table which those values (1, 'user1', 1) (2, 'user2', 6) What I would like to get is a s...

group by SKU with count

I have a table with parts in it: parts (partID, sku, ....) The SKU looks like: ABC1232 ABC1332 DSE234 XYZ322 XYZ332 etc... I need to group by manufacturer, so I have to get a substring of the SKU, taking the first 3 characters and then grouping them together and getting a count of them. So the resulting output needs to look like: ...

Getting additional info on the result of a SQL max query

Say I want to do this with SQL (Sybase): Find all fields of the record with the latest timestamp. One way to write that is like this: select * from data where timestamp = (select max(timestamp) from data) This is a bit silly because it causes two queries - first to find the max timestamp, and then to find all the data for that timest...

Optimal key for MySQL / InnoDB table with multi-part WHERE

Given a (simplified) table CREATE TABLE `transactions` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `CREATION_DT` datetime DEFAULT NULL, `STATUS_IND` int(11) DEFAULT NULL, `COMPANY_ID` bigint(20) DEFAULT NULL, `AMOUNT` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`), KEY `FKE7E81F1ED170D4C9` (`COMPANY_ID`), KEY `RPTIDX_CREATI...

Select the X "closest" ids ?

Hello, Let's say you have a table with a integer primary key named 'table_id' Is it possible in a single query to extract the row with a specific id AND the X row before it AND the X row after it ? For exemple, if your ids are (1,2,8,12,16,120,250,354), X is 2 and the id you have is 16, the select should return rows with ids 8,12,16,1...

What do we consider as a dynamic sql statement?

a) What do we consider as a dynamic sql statement? Any sql statement that dynamically adds a clause(s) or even just a part of a clause to a SQL string? b)Aren’t then parameterized strings that use placeholders for dynamically supplied values also considered dynamic sql statements? thanx ...

Which will be faster out of these two queries?

SELECT * FROM table WHERE col IN (1,2,3) or SELECT * FROM table WHERE col = 1 OR col = 2 OR col = 3 ...

Is get Property called on construction or only when the Property is called?

I have a piece of code that I would like to set as part of the get {} section of a property. However, inside this section I call a SQL query. I would like to know if the call will be made when the object is constructed (i.e. Object t = new Object()) or only when the property is called (i.e. string query = Object.SqlQuery). ...

How do you remove a table COMMENT in MySQL?

We have a MySQL table that was altered with ALTER TABLE t COMMENT 'foo' we later realize that we don't want that comment. Is there a way to delete it? simply saying ALTER TABLE t COMMENT 'NOT foo' simply adds an additional comment, so that when you do a SHOW CREATE TABLE t it shows BOTH comments... ETA: AH, the problem seem...

Setting Up SQL Server Session Database

I notice that the sql to create the SQL server database (InstallPersistSqlState.sql) basically creates the database ASPState. If I wanted to name that different, can I just replace that with my prefered database name? I understand the script, I just wanted to make sure that ASPState didn't have to be the database name. Thanks! ...

Query Concatenated Field (using SubSonic)

Is there a way to query against a concatenated field using MS SQL? For instance, what I want to do is something like: Select FirstName+' '+LastName as FullName from Attendees where FullName like '%Joe Schmoe%' The above doesn't work. What I have found works is: Select * from Attendee where FirstName+' '+LastName like '%Joe Schmoe%' ...

How to search a numeric value in a database

Let say i have querry structured like below which indicates dates search.asp?date=091210 I want to find each record that includes "0912" string how can i inquire it on the link structure. ...

Zend DB Select with multiple table joins

Hey all, Trying to replicate the following query using Zend_Db_Select. Any pointers? SELECT compounds.id as compounds_id, reactions.id as reactions_id, reaction_compound.number as reaction_compound_number FROM compounds, reactions, reaction_compound WHERE compounds.id IN (68,74,112) AND compounds.id = reaction_compoun...