I’ve got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I’ve thought of a complicated way, creating a temp table with a “random number” column, …
Tag: sql
Getting new IDs after insert
I’m inserting a bunch of new rows into a table which is defined as follows: using the following insert: when I’ve finished, I’d like to know the IDs of all the newly inserted rows. SCOPE_IDENTITY() only returns the ID last row inserted. How can I get all the new IDs? One method that springs …
WHERE IS NULL, IS NOT NULL or NO WHERE clause depending on SQL Server parameter value
I have a stored procedure in SQL Server 2000 that performs a search based on parameter values. For one of the parameters passed in, I need a different WHERE clause depending on its value – the problem …
How can I compare time in SQL Server?
I’m trying to compare time in a datetime field in a SQL query, but I don’t know if it’s right. I don’t want to compare the date part, just the time part. I’m doing this: Is it correct? I’m asking this because I need to know if 08:00:00 is less or greater than 07:30:00 and I…
How to update rows with a random date
I have a simple SQL table which has a DateTime column. I would like to update all the rows (>100000 rows) with a random date. Is there a simple way to do this a SQL Query?
How to use “AND” in a Django filter?
How do I create an “AND” filter to retrieve objects in Django? e.g I would like to retrieve a row which has a combination of two words in a single field. For example the following SQL query does exactly that when I run it on mysql database: How do you accomplish this in Django using filters? Answe…
What is the equivalent of ‘go’ in MySQL?
In TSQL I can state: In MySQL I can’t write the same query. What is the correct way to write this query in MySQL? Answer Semicolon at the end of the statement.
What’s the fastest way to do a bulk insert into Postgres?
I need to programmatically insert tens of millions of records into a Postgres database. Presently, I’m executing thousands of insert statements in a single query. Is there a better way to do this, some bulk insert statement I do not know about? Answer PostgreSQL has a guide on how to best populate a dat…
MS Sql: Conditional ORDER BY ASC/DESC Question
I want to to make to make the ordering in my query conditional so if it satisfiess the condition it should be ordered by descending For instance: Answer Don’t change the ASC or DESC, change the sign of the thing being sorted-by: The OP asks: Guys, I am not the SQL Expert, please explain me what means th…
Are there multiline comment delimiters in SQL that are vendor-agnostic?
While editing, building up, or cherry picking from SQL statements I can find myself interacting with one of four popular database tools. I have been resorting to single line commenting for DB2 and Informix. I have created macros in Vim to make this slightly more efficient, but I was wondering if I am working …