Forum Replies Created

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

  • 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

  • RE: Can I join on an alias?

    You can. It's hard to work with what you posted so I created the following sample code to demonstrate how you would join two tables in the way you are...

    "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: delete top 100

    As Sean said, you need an ORDER BY statement.

    I created some sample data and a query that you could use for reference. I am a fan of using the...

    "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: Index and Table Size

    My object naming needs to be cleaned up a little but I think you are looking for something like this:

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

    DROP TABLE #tableDataPrep;

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

    DROP...

    "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: capture currently logged in users from sql

    You can do something like what I did below. I'm not 100% clear about what you are trying to do but this should help....

    You would have an agent job run...

    "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,458 total)