Forum Replies Created

Viewing 15 posts - 2,176 through 2,190 (of 2,462 total)

  • RE: query to get the size of all indexes in a database

    You could do something like this:

    IF OBJECT_ID('tempdb..#indexInfo') IS NOT NULL

    DROP TABLE #indexInfo;

    SELECT TOP 0

    t.TABLE_CATALOG AS db,

    t.TABLE_SCHEMA AS SchemaName,

    OBJECT_NAME(i.OBJECT_ID) AS TableName,

    ...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Effective T-SQL for Median

    TheSQLGuru (7/30/2013)


    Fortunately for you SQL 2012 has much better Windowing Function support. Look into the PERCENTILE_CONT function. I also HIGHLY recommend you purchase Itzik Ben-Gan's SQL Server 2012...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Effective T-SQL for Median

    dkschill (7/30/2013)


    Is this an effective way to find median?

    CREATE TABLE #T (i TINYINT);

    GO

    INSERT INTO #T

    SELECT ABS(CHECKSUM(NEWID())) % 250

    GO 1000

    ;WITH t AS (SELECT i, ROW_NUMBER() OVER (ORDER BY i) AS n,...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Need T-SQL Query Help- Urgent

    ChrisM@Work (7/29/2013)


    Thanks for the very generous feedback, Mr Kapsicum.

    If you're interested in how the method works, here's an excellent article by Dwain Camps[/url].

    I have nothing to add to this thread...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Find the last 6 Tuesdays or Wed or Whatever day.

    Using this sample data:

    --Adjust for the number of days of sample data you would like

    DECLARE @days int = 1000;

    IF OBJECT_ID('tempdb..#sales') IS NOT NULL

    DROP TABLE #sales;

    CREATE TABLE #sales

    (sale_id int identity primary...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Why SQL Server converts '' to zero

    I asked this same question a few months back and got some good replies; just thought I'd share this:

    http://www.sqlservercentral.com/Forums/Topic1397471-391-1.aspx

    Check this out:

    SELECT CAST(REPLICATE(CAST('' AS int),10) AS char(10)) AS...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: using the format() command

    Tobar (7/25/2013)


    Anyone point me to the documentation, or explain why you would expect these results?

    DECLARE @d DATETIME = '07/25/2013 10:11:12.345';

    SELECT FORMAT ( @d, 'yyyy dd mm hh mm ss')

    2013 25...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: SQL Distinct comma delimited list

    Eugene Elutin (7/25/2013)


    Alan.B (7/24/2013)


    Erland Sommarskog (7/24/2013)


    Alan.B (7/24/2013)


    Why is the SELECT

    @x=@x+ method not guaranteed to work?

    Why would it?

    See this KB article Pay particular attention to the first sentence under Cause.

    I...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: SQL Distinct comma delimited list

    Erland Sommarskog (7/24/2013)


    Alan.B (7/24/2013)


    Why is the SELECT

    @x=@x+ method not guaranteed to work?

    Why would it?

    See this KB article Pay particular attention to the first sentence under Cause.

    I say it would...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Case statement

    This would do the trick:

    SELECT

    CASE

    WHEN ISNULL(rate,100)=100 THEN 1

    ELSE 0

    END AS Rate

    FROM {yourtable}

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: SQL Distinct comma delimited list

    Erland Sommarskog (7/24/2013)


    SELECT substring(list, 1, len(list) - 1)

    FROM (SELECT list =

    (SELECT DISTINCT name + ','

    ...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Concatenation

    A couple techniques:

    SELECT RIGHT(abc_col1,len(abc_col1)-2)

    FROM tableA

    SELECT REPLACE(abc_col1,'SH','')

    FROM tableA

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: SQL Distinct comma delimited list

    Here's another way... it's kind of a "Quirky-Coalesce":

    -- sample data

    DECLARE @Production_Product TABLE (name varchar(10));

    INSERT INTO @Production_Product VALUES ('aaa'),('bbb'),('ccc'),('ccc');

    DECLARE @listStr varchar(max)='';

    SELECT @listStr=@listStr+

    CASE

    WHEN @listStr='' THEN ''+name

    ELSE ','+name

    END

    FROM (SELECT DISTINCT name FROM...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Edit multiple stored procedures

    NineIron (7/24/2013)


    I need to create 5 test databases by copying the live db's. There are many stored procedures in each db. Each sp is told which db to use, Use...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • RE: Can I join on an alias?

    Luis Cazares (7/18/2013)


    No, you can't because the alias doesn't exist when the JOIN is processed.

    More info: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/70efeffe-76b9-4b7e-b4a1-ba53f5d21916/order-of-execution-of-sql-queries

    You can get around that doing something like this (using my own sample code...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 15 posts - 2,176 through 2,190 (of 2,462 total)