I need to convert a SQL Server DATETIME value to FILETIME in a T-SQL SELECT statement (on SQL Server 2000). Is there a built-in function to do this? If not, can someone help me figure out how to implement this conversion routine as a UDF (or just plain Transact-SQL)? Here is what I know:
FILETIME is 64-bit value representing the numbe...
I'm trying to construct a T-SQL statement with a WHERE clause determined by an input parameter. Something like:
SELECT * FROM table
WHERE id IN
CASE WHEN @param THEN
(1,2,4,5,8)
ELSE
(9,7,3)
END
I've tried all combination of moving the IN, CASE etc around that I can think of. Is this (or something like it) possible?
...
I just inherited a project that has code similar to the following (rather simple) example:
DECLARE @Demo TABLE
(
Quantity INT,
Symbol NVARCHAR(10)
)
INSERT INTO @Demo (Quantity, Symbol)
SELECT 127, N'IBM'
My interest is with the N before the string literal.
I understand that the prefix N is to specify encoding (in this case,...
Hi, I have thought about the following SQL statements:
INSERT INTO A(a1, a2)
SELECT b1, udf_SomeFunc(b1)
FROM B
Where udf_SomeFunc makes a select on table A. As I understand, first, a shared lock is set on A (I am talking just about table A now), then, after this lock is released, an exclusive lock is obtained to insert the data. The ...
What would be the best approach to combine the two results sets in one result set in T-SQL?
SQL statment #1:
SELECT
COUNT(t.col1) as 'Number of Responses',
t.col2 as 'Department'
FROM table t
WHERE col3 IS NOT NULL
GROUP BY t.col1
ORDER BY t.col1
SQL Statment #1:
SELECT
COUNT(t.col1) as 'Total number of participants...
Based on following AreaState table
Area State
-------------------
A1 Active
A1 Active
A1 Active
A1 Proposed
A1 Proposed
A2 Active
A2 Proposed
I want to write a stored proc that returns count of state for each of the areas. Input to the stored proc is any valid State (in this case @state is the input parameter). I was hoping ...
UPDATE TableA
SET Value = a.Value * b.AnotherValue
FROM TableA AS a
INNER JOIN TableB AS b
WHERE (Condition is true);
Here is the problem. The Value field for TableA does not allow nulls. If the calculation of a.Value * b.AnotherValue yields a null, an error is thrown. Now the question. Is there any way to tell the UPDATE to ignore the...
Say I have a table with a field called "ordernum" that denotes the order of a given set of rows. Now imagine that I delete one of these rows. What type of query would work best for re-assigning the order numbers so that they remain sequential?
Here's an example:
id group_id name ordernum active
----------------...
Is there a freely available GUI tool that will allow interaction with Microsoft SQL Server (via T-SQL) that provides an auto-format feature?
I constantly find myself writing queries in SQL Query Analyzer (Microsoft’s standard GUI tool for T-SQL) and cutting/pasting the whole thing into SQLyog (a GUI tool for MySQL), where I can press F1...
I have a query with about 6-7 joined tables and a FREETEXT() predicate on 6 columns of the base table in the where.
Now, this query worked fine (in under 2 seconds) for the last year and practically remained unchanged (i tried old versions and the problem persists)
So today, all of a sudden, the same query takes around 1-1.5 minutes.
...
When you create a foreign key constraint in a table and you create the script in MS SQL Management Studio, it looks like this.
ALTER TABLE T1 WITH CHECK ADD CONSTRAINT FK_T1 FOREIGN KEY(project_id)
REFERENCES T2 (project_id)
GO
ALTER TABLE T1 CHECK CONSTRAINT FK_T1
GO
What I don't understand is what purpose has the second alter with...
Hi,
I'm using Sql Server 2005 and i'm trying to achieve something like:
In the same select statement i want to get the first x rows and the last x rows.
SELECT TOP(5) BOTTOM(5)
Of course 'bottom' does not exist so i need other solution.
I believe there is an easy and elegant solution that i'm not getting.
Doing the select again with...
Sorry for an unclear question previously; hopefully I can start again...
I have this data:
entityid name stringvalue
----------- -------------------- --------------------
1 ShortDescription Coal
1 LongDescription BlackCoal
1 ShortDescription Gold
1 LongDescription ...
I need to know how to interrogate my Microsoft SQL Server to know if a given database has been set to read only or not. Is that possible?
...
I have a table for logging that needs a log ID but I can't use an identity column because the log ID is part of a combo key.
create table StuffLogs
{
StuffID int
LogID int
Note varchar(255)
}
There is a combo key for StuffID & LogID.
I want to build an insert trigger that computes the next LogID when inserting log records. ...
How can I check if any column in a given table only have null or empty string values? Can I in some way extend this for every table in the database?
...
EDIT: my apologies, this was a MSSQL2008 issue.
I have a SQL problem that I've come up against routinely, and normally just solved w/ a nested query. I'm hoping someone can suggest a more elegant solution.
It often happens that I need to select a result set for a user, conditioned upon it being the most recent, or the most sizeable...
Based on the following table
Area S1 S2 S3 S4
--------------------
A1 5 10 20 0
A2 11 19 15 20
A3 0 0 0 20
I want to generate an output that will give the number of columns not having "0".
So the output would be
Area S1 S2 S3 S4 Count
-------------------------
A1 5 10 20 0 3
A2 11 19 15 20 4
A3 0 ...
If I have a table with a schema like this
table(Category, SubCategory1, SubCategory2, Status)
I would like to group by Category, SubCategory1 and aggregate the Status such that
if not all Status values over the group have a certain value Status will be 0 otherwise 1.
So my result set will look like
(Category, SubCategory1, Status)
...
Is it possible to pass a SQL script to some method that Entity Framework has to run it against my model? e.g. equivalent of:
context.ExecuteStoreCommand(<tsql script path>);
Background: I want a way to reset the database during unit tests, and making a call to run the EF generated TSQL script (from Generate Database from Model) seem...