SQLServerCentral - www.sqlservercentral.com

A community of more than 1,600,000 database professionals and growing

Featured Contents

The Voice of the DBA

Short Names

I started using computers a long time ago, and in the PC world, we were often limited to an 8 character name and a 3 character extension. Unix and MacOS allowed longer names, and many of us in DOS and Windows were jealous. Eventually Windows evolved, allowing long names, spaces, and really quite a bit of latitude in what you want to name a file. For example, try this and see what happens on your SQL Server:

 BACKUP DATABASE Sandbox TO DISK = 'Sandbox.thisisafullbackupthatIusetostartarestore'

However, the three character extension still dominates, and many applications still use this. Microsoft has started to get away from this, as we have .docx, .xlsx, etc. Other vendors and software systems have started to expand names slightly. I was reading about the SQL Server Diagnostics Preview and noticed that the engineers will take a dump file (.dmp) as well as a mini dump (.mdmp) and a filtered dump (.hdmp).

Now I know developers are lazy, and they don't like to type, but in these days of auto-completion and other tools, why are we limiting ourselves. Why wouldn't we use .minidump or .filtereddump as descriptive way of identifying the file? If we are no longer bound, why not include a better extension? I can't imagine that the filesystem for many tools would be stressed by longer names.

I'm assuming that people still feel bound to using the shortest set of letters that they think are unique, but with the growth of software applications from many, many sources, why not just be more descriptive? Would you want to see better filenames? I know I would.

Steve Jones from SQLServerCentral.com

Join the debate, and respond to today's editorial on the forums


The Voice of the DBA Podcast

Listen to the MP3 Audio ( 2.7MB) podcast or subscribe to the feed at iTunes and Libsyn. feed

The Voice of the DBA podcast features music by Everyday Jones. No relation, but I stumbled on to them and really like the music.

ADVERTISEMENT
SQL Compare

The industry standard for comparing and deploying SQL Server database schemas

Trusted by 71% of Fortune 100 companies, SQL Compare is the fastest way to compare changes, and create and deploy error-free scripts in minutes. Plus you can easily find and fix errors caused by database differences. Download your free trial

Database DevOps

Database DevOps Demo Webinar

Learn how to automate your database deployments alongside your app code in this free demo webinar. Register now

Featured Contents

 

Azure DWH part 11: Data Warehouse Migration Utility

Daniel Calbimonte from SQLServerCentral.com

There is a new utility to import data from SQL Server on premises or Azure SQL to Azure SQL Data Warehouse (ASDW) More »


 

Getting Started with Azure SQL Data Warehouse - Part 6

Additional Articles from Database Journal

Azure SQL Data Warehouse is Microsoft’s new enterprise-class, elastic petabyte-scale, data warehouse service that can scale according to organizational demands in just few minutes. In this installment, Arshad Ali covers the different ways to load data into SQL Data Warehouse. More »


 

From the SQLServerCentral Blogs - Security Basics: The Principle of Least Privilege

Brian Kelley from SQLServerCentral Blogs

Whenever I’m asked about creating a security model for an application or database, I tell folks to follow the Principle... More »


 

From the SQLServerCentral Blogs - I Love Entity Framework

Grant Fritchey from SQLServerCentral Blogs

I love Entity Framework. I also like (not love) nHibernate. That’s right, as a DBA and data professional, I’m telling... More »

Question of the Day

Today's Question (by Steve Jones):

I've got a LineItem table iwth some quanties, prices, and discount percentages. The data looks like this:

 LineItemKey Qty Price DiscountPercentage ----------- ----------- ----------- ------------------ 1 10 5 0 2 25 8 5 3 100 11 10 

I am trying to write a query that will give me the final price.

 SELECT LineItemKey, Qty, Price, DiscountPercentage, FinalCost = CAST((Qty * Price) - (DiscountPercentage / 100) AS DECIMAL(10, 2)) FROM dbo.LineItem; 

However, I get this result:

 LineItemKey Qty Price DiscountPercentage FinalCost ----------- ----------- ----------- ------------------ --------------------------------------- 1 10 5 0 50.00 2 25 8 5 200.00 3 100 11 10 1100.00

Why?

Think you know the answer? Click here, and find out if you are right.


We keep track of your score to give you bragging rights against your peers.
This question is worth 1 point in this category: T-SQL.

We'd love to give you credit for your own question and answer.
To submit a QOTD, simply log in to the Contribution Center.

ADVERTISEMENT

Expert Scripting and Automation for SQL Server DBAs

Automate your workload and manage more databases and instances with greater ease and efficiency by combining metadata-driven automation with powerful tools like PowerShell and SQL Server Agent. Automate your new instance-builds and use monitoring to drive ongoing automation, with the help of an inventory database and a management data warehouse. Get your copy from Amazon today.

Yesterday's Question of the Day

Yesterday's Question (by Thomas Franz):

Which of the following SELECTs will return a line (select 9)?

 CREATE TABLE #test (id INT IDENTITY, some_stuff SQL_VARIANT); INSERT INTO #test (some_stuff) VALUES ('hey') INSERT INTO #test (some_stuff) VALUES (123) GO SET NOCOUNT ON; DECLARE @stuff SQL_VARIANT; /* 1*/ SET @stuff = 'hey'; SELECT 1, t.some_stuff FROM #test AS t WHERE t.some_stuff = @stuff /* 2*/ SET @stuff = '123'; SELECT 2, t.some_stuff FROM #test AS t WHERE t.some_stuff = @stuff /* 3*/ SET @stuff = 123 ; SELECT 3, t.some_stuff FROM #test AS t WHERE t.some_stuff = @stuff DECLARE @string VARCHAR(3); /* 4*/ SET @string = 'hey'; SELECT 4, t.some_stuff FROM #test AS t WHERE t.some_stuff = @string /* 5*/ SET @string = '123'; SELECT 5, t.some_stuff FROM #test AS t WHERE t.some_stuff = @string /* 6*/ SET @string = 123 ; SELECT 6, t.some_stuff FROM #test AS t WHERE t.some_stuff = @string /* 7*/ SET @string = 'hey'; SELECT 7, t.some_stuff FROM #test AS t WHERE t.some_stuff = cast(@string as sql_variant) /* 8*/ SET @string = '123'; SELECT 8, t.some_stuff FROM #test AS t WHERE t.some_stuff = cast(@string as sql_variant) /* 9*/ SET @string = 123 ; SELECT 9, t.some_stuff FROM #test AS t WHERE t.some_stuff = cast(@string as sql_variant) /*10*/ SET @string = 'hey'; SELECT 10, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @string /*11*/ SET @string = '123'; SELECT 11, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @string /*12*/ SET @string = 123 ; SELECT 12, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @string /*13*/ SET @stuff = 'hey'; SELECT 13, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @stuff /*14*/ SET @stuff = '123'; SELECT 14, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @stuff /*15*/ SET @stuff = 123 ; SELECT 15, t.some_stuff FROM #test AS t WHERE CAST(t.some_stuff AS VARCHAR(3)) = @stuff

Answer:

  • 1
  • 3
  • 4
  • 7
  • 10
  • 11
  • 12
  • 13
  • 14

Explanation:

1 - comparing a SQL_VARIANT containing a varchar ('hey') with another SQL_VARIANT containing a varchar ('hey) will be ok
 2 - comparing a SQL_VARIANT containing an integer (123) with another SQL_VARIANT containing a varchar ('123') will fail
 3 - comparing a SQL_VARIANT containing an integer (123) with another SQL_VARIANT containing a integer (123) will be ok
 
 4 - comparing a SQL_VARIANT containing a varchar ('hey') with a VARCHAR(3) ('hey') will be ok
 5 - comparing a SQL_VARIANT containing an integer (123) with a VARCHAR(3) ('123') will fail; neither the 123 on table side will not be converted implicit to varchar nor the '123' to integer
 6 - comparing a SQL_VARIANT containing an integer (123) with a VARCHAR(3) (123) will fail too, since the integer 123 in the set statement will be converteted implicit into '123' (= same behavior as in answer 5)
 
 7 - comparing a SQL_VARIANT containing a varchar ('hey') with an explicit converted SQL_VARIANT ('hey') will be ok
 8 - comparing a SQL_VARIANT containing an integer (123) with varchar that was converted into SQL_VARIANT will fail, since the SQL_VARIANT contains the varchar '123'
 9 - comparing a SQL_VARIANT containing an integer (123) with varchar that was converted into SQL_VARIANT will fail, since the SQL_VARIANT contains the varchar '123' (in the SET statement 123 was converted implicit into '123')

10 - comparing an explicit convertet varchar ('hey') with another varchar ('hey') will be ok
11 - comparing an explicit convertet varchar (123 -> '123') with another varchar ('123') will be ok
12 - comparing an explicit convertet varchar (123 -> '123') with another varchar ('123') will be ok (in the SET statement 123 was converted implicit into '123')

13 - comparing an explicit convertet varchar ('hey') with a SQL_VARIANT containing a varchar ('hey') will be ok (similar to answer 1)
14 - comparing an explicit convertet varchar (123 -> '123') with a SQL_VARIANT containing a varchar ('123') will be ok
15 - comparing an explicit convertet varchar (123 -> '123') with a SQL_VARIANT containing an integer (123) will fail (because of datatype mismatch)

So when you have to handle SQL_VARIANTS (e.g. when working with sys.partition_range_values or sys.extended_properties), you have to ensure, that your variable is a SQL_VARIANT too and was filled with exact the same data type (answers 1 and 3).

If this is not possible (since you do not know, if the integers where inserted as integer or as varchar) the best solution (imho) would be to explicit convert the SQL_VARIANT on the table side into a VARCHAR and compare it with a VARCHAR variable (answers 10 to 12).
Remark: if you have an index on the SQL_VARIANT column, it would do an index scan (instead of seek) in this case, since the explicit convert will make it non-sargable. And of course you have to be careful when dealing with date or decimal datatypes (because of language settings).

See click here


» Discuss this question and answer on the forums

Database Pros Who Need Your Help

Here's a few of the new posts today on the forums. To see more, visit the forums.

SQL Server 2016 : SQL Server 2016 - Administration

can't shrink tempdb files because it is a work table page. - Hello All, I tried to shrink my tempdb data files but didn't succeeded. i got this message: "DBCC SHRINKFILE: Page 1:34383801 could...

Always on - Hi, i need to setup the always on my databases .i need to design the infrastructure .can any one suggest server...


SQL Server 2016 : SQL Server 2016 - Development and T-SQL

BULK Insert + create temperory table at the time of insert - How to Create temp table at  the time of insert? DECLARE @sql AS NVARCHAR(1000) SET @sql = 'BULK INSERT ' + #tableName + ' FROM ''' + @filename + ''' WITH...


SQL Server 2014 : Administration - SQL Server 2014

Index/Statistic Order - Hi all. I've wrote a blog post to ask a question on how statistics are created for new indexes. Essentially I...


SQL Server 2014 : Development - SQL Server 2014

Same query different Execution plan on two different server - Same database, same tables, same query but running on two different servers bringing two different execution plans. plan1_server1 gets executed...

Separate rows depend on Previous Row - I have table with data like the below : ID Name  Category_Sort 1 Campaign1  Beverage 2 Campaign2  Beverage 3 Campaign3  Food I need to select...


SQL Server 2012 : SQL 2012 - General

ORDER BY THEN COUNT - UNTIL 1ST STATUS - Hi I have the below problem. I am trying to see how often a customer has requested Re-Activation of their Internet account. The...

Grouping to Total - Dept#             TransCode          TCost          OfficeCode          OffCost                 Totalcost  777


SQL Server 2012 : SQL Server 2012 - T-SQL

If exists table dynamic sql from variable database id - Hi  I want to write if the table exists in the database in dynamic sql then select from that table . I...

Distinct values from 2 columns - I want to get the distinct values from this table as row no 3 and 10 are the same .I...

Using Queues - I have searched for the last 4 hours looking for how to implement a simple queue system in SQL Server....


SQL Server 2008 : SQL Server 2008 - General

Best way to push records into Websphere mq - Hi, I have a table which gets around 20 Mln records (transactions) per day.  I need around 10 Mln of them...

Install SSIS on an existing Cluster - SQL 2008 R2 - Hi Guys Have been asked to install SSIS on a cluster This is SQL 2008 R2 Have found the instructions to...

Partitioned views error - not updatable - I have created a partitioned view which will be union all across different databases but the table name is same....


SQL Server 2008 : T-SQL (SS2K8)

How to Calculate Difference between 2 queries Columns in union ALL - Hello Good Afternoon, Can you please help me with below query how to get the another column in the output as...


SQL Server 2008 : SQL Server 2008 High Availability

Fail over failed - Hi All,   We did fail over on one of our serv


Reporting Services : Reporting Services

Career - Hi i have total 5 year exp. 4years in SSRS and SQL 3years ssis. 1 year in SSAs. I am currently working on ssrs...

This email has been sent to newsletter@newslettercollector.com. To be removed from this list, please click here.
If you have any problems leaving the list, please contact the webmaster@sqlservercentral.com.
This newsletter was sent to you because you signed up at SQLServerCentral.com.
Feel free to forward this to any colleagues that you think might be interested.
If you have received this email from a colleague, you can register to receive it here.
This transmission is ©2017 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved.
Contact: webmaster@sqlservercentral.com