Showing posts with label tsql. Show all posts
Showing posts with label tsql. Show all posts

26 February 2011

Creating a SQL update statement that incorporates an inner join

This example uses both an INNER JOIN (to update the values in Table1 with the corresponding values from Table2) and aliasing for readability.

  1. I added the additional parameters in the WHERE for illustrative purposesUPDATE t1
    SET t1.Column2 = t2.Column2
    FROM dbo.Table1 t1
    INNER JOIN dbo.Table2 t2
    ON t1.Column1 = t2.Column1
    WHERE
    AND t1.Column3 = value
    AND t2.Column4 = value

06 January 2008

How do I select the name for a day of the week?

Selecting the name for a day of the week is fairly straight forward using the DATENAME function and a DATEPART argument. For example:

SELECT DATENAME(weekday, GETDATE()) AS 'MyDay';
returns:

MyDay
------
Sunday

We could also use DATENAME to obtain the name of a month:

SELECT DATENAME(month, GETDATE()) AS 'MyMonth';
Which returns:

MyDay
-------
January

For additional information, please visit: http://msdn2.microsoft.com/en-us/library/ms174395.aspx

How do I select only the date from a datetime data type?

Selecting only the date portion from a datetime data type could be accomplished in a number of ways. One of the most convenient methods is to simply use CONVERT and one of the many style values.

SELECT GETDATE() AS MyDate
returns the default format:

MyDate
-----------------------
2008-01-06 19:39:15.410

However, buy using CONVERT and applying a particular style value, we are able to achieve the desired result. For example:

SELECT CONVERT(VARCHAR(10),GETDATE(),126) AS MyDate
returns:

MyDate
----------
2008-01-06

Another style example:

SELECT CONVERT(VARCHAR(10),GETDATE(),101) AS MyDate
returns:

MyDate
----------
01/06/2008

For additional information, visit: http://msdn2.microsoft.com/en-us/library/aa226054(SQL.80).aspx

05 January 2008

How do I concatenate characters or strings using SQL?

Concatenation (joining 2 or more characters or strings of the same data type) is accomplished using the "+" operator. For example, let's say that I want to join the strings "Able" and "Baker". To do so, I would simply write:

select 'Windows' + 'Vista' as OSName
Running this query would return:

OSName
------------
WindowsVista

OK so far, but what if you want a space between "Windows" and "Vista"? That could be accomplished in 2 ways: Either by including it in one of our existing strings (i.e. "Windows " or " Vista"), as in:

select 'Windows ' + 'Vista' as OSName
or by adding an additional concatenation operator:

select 'Windows' + ' ' + 'Vista' as OSName
Both methods return the desire result:

OSName
-------------
Windows Vista

A more useful example that also demonstrates the "of the same data type" caveat mentioned above:

select 'Today is: ' + convert(varchar(10), getdate(), 101) as Today
would return:

Today
--------------------
Today is: 01/05/2008

However, if we failed to convert result of getdate() into a varchar, we would receive an error similar to:

Today
----------------------
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.

For additional information, please visit: http://msdn2.microsoft.com/en-us/library/ms177561.aspx