sql-server-2005

How to get the last record per group in SQL

最后都变了- 提交于 2019-12-17 03:20:07
问题 I am facing a rather interesting problem. I have a table with the following structure: CREATE TABLE [dbo].[Event] ( Id int IDENTITY(1,1) NOT NULL, ApplicationId nvarchar(32) NOT NULL, Name nvarchar(128) NOT NULL, Description nvarchar(256) NULL, Date nvarchar(16) NOT NULL, Time nvarchar(16) NOT NULL, EventType nvarchar(16) NOT NULL, CONSTRAINT Event_PK PRIMARY KEY CLUSTERED ( Id ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS

How much size “Null” value takes in SQL Server

谁都会走 提交于 2019-12-17 03:02:06
问题 I have a large table with say 10 columns. 4 of them remains null most of the times. I have a query that does null value takes any size or no size in bytes. I read few articles some of them are saying : http://www.sql-server-citation.com/2009/12/common-mistakes-in-sql-server-part-4.html There is a misconception that if we have the NULL values in a table it doesn't occupy storage space. The fact is, a NULL value occupies space – 2 bytes SQL: Using NULL values vs. default values A NULL value in

Login failed for user 'DOMAIN\MACHINENAME$'

痴心易碎 提交于 2019-12-17 02:55:31
问题 I know this is almost a duplicate of : The error "Login failed for user 'NT AUTHORITY\IUSR'" in ASP.NET and SQL Server 2008 and Login failed for user 'username' - System.Data.SqlClient.SqlException with LINQ in external project / class library but some things don't add up compared to other appliations on my server and I am not sure why. Boxes being used: Web Box SQL Box SQL Test Box My Application: I have an ASP.NET Web Application, which references a class library that uses LINQ-to-SQL.

Search for a string in all tables, rows and columns of a DB

这一生的挚爱 提交于 2019-12-17 02:41:10
问题 I am lost in a big database and I am not able to find where the data I get comes from. I was wondering if it is possible with SQL Server 2005 to search for a string in all tables, rows and columns of a database? Does anybody has an idea if it is possible and how? 回答1: This code should do it in SQL 2005, but a few caveats: It is RIDICULOUSLY slow. I tested it on a small database that I have with only a handful of tables and it took many minutes to complete. If your database is so big that you

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

做~自己de王妃 提交于 2019-12-17 02:33:26
问题 Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)? I know that I can either Use a DELETE without a where clause and then RESEED the identity (or) Remove the FK, truncate the table, and recreate the FK. I thought that as long as I truncated the child table before the parent, I'd be okay without doing either of the options above, but I'm getting this error: Cannot truncate table

Turn off constraints temporarily (MS SQL)

江枫思渺然 提交于 2019-12-17 02:26:22
问题 I'm looking for a way to temporarily turn off all DB's constraints (eg table relationships). I need to copy (using INSERTs) one DB's tables to another DB. I know I can achieve that by executing commands in proper order (to not break relationships). But it would be easier if I could turn off checking constraints temporarily and turn it back on after the operation's finish. Is this possible? 回答1: You can disable FK and CHECK constraints only in SQL 2005+ . See ALTER TABLE ALTER TABLE foo

Integer division in sql server

淺唱寂寞╮ 提交于 2019-12-17 02:17:09
问题 In Microsoft SQL Server 2005, why do the following commands produce integer results? SELECT cast(151/6 AS DECIMAL(9,2)) SELECT 151/6 回答1: In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected. If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result. SELECT 151/CAST(6 AS DECIMAL (9,2)) 回答2: Yes that is standard behavior do SELECT 151/6

How do I perform an accent insensitive compare (e with è, é, ê and ë) in SQL Server?

放肆的年华 提交于 2019-12-17 02:10:50
问题 I'm looking to compare two varchars in SQL, one would be something like Cafe and the other Café is there a way in SQL that will allow the two values to be compared. For instance: SELECT * FROM Venue WHERE Name Like '%cafe%' So if there is a venue with the name Big Bobs Café Extraordinaire it would be included in the result set? 回答1: Coerce to an accent insensitive collation You'll also need to ensure both side have the same collation to avoid errors or further coercions if you want to compare

How to get Time from DateTime format in SQL?

蹲街弑〆低调 提交于 2019-12-16 22:12:27
问题 I want to get only Time from DateTime column using SQL query using SQL Server 2005 and 2008 Default output: AttDate == 2011-02-09 13:09:00 2011-02-09 14:10:00 I'd like this output: AttDate Time == 2011-02-09 13:09:00 13:09 2011-02-09 14:10:00 14:10 回答1: SQL Server 2008: select cast(AttDate as time) [time] from yourtable Earlier versions: select convert(char(5), AttDate, 108) [time] from yourtable 回答2: Assuming Sql server SELECT CONVERT(VARCHAR(8),GETDATE(),108) 回答3: SQL Server 2008+ has a

How to get Time from DateTime format in SQL?

女生的网名这么多〃 提交于 2019-12-16 22:12:23
问题 I want to get only Time from DateTime column using SQL query using SQL Server 2005 and 2008 Default output: AttDate == 2011-02-09 13:09:00 2011-02-09 14:10:00 I'd like this output: AttDate Time == 2011-02-09 13:09:00 13:09 2011-02-09 14:10:00 14:10 回答1: SQL Server 2008: select cast(AttDate as time) [time] from yourtable Earlier versions: select convert(char(5), AttDate, 108) [time] from yourtable 回答2: Assuming Sql server SELECT CONVERT(VARCHAR(8),GETDATE(),108) 回答3: SQL Server 2008+ has a