- I added the additional parameters in the WHERE for illustrative purposes
UPDATE 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
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.
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:
returns:
We could also use DATENAME to obtain the name of a month:
Which returns:
For additional information, please visit: http://msdn2.microsoft.com/en-us/library/ms174395.aspx
SELECT DATENAME(weekday, GETDATE()) AS 'MyDay';returns:
MyDay
------
SundayWe could also use DATENAME to obtain the name of a month:
SELECT DATENAME(month, GETDATE()) AS 'MyMonth';Which returns:
MyDay
-------
JanuaryFor 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.
returns the default format:
However, buy using CONVERT and applying a particular style value, we are able to achieve the desired result. For example:
returns:
Another style example:
returns:
For additional information, visit: http://msdn2.microsoft.com/en-us/library/aa226054(SQL.80).aspx
SELECT GETDATE() AS MyDatereturns the default format:
MyDate
-----------------------
2008-01-06 19:39:15.410However, 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 MyDatereturns:
MyDate
----------
2008-01-06Another style example:
SELECT CONVERT(VARCHAR(10),GETDATE(),101) AS MyDatereturns:
MyDate
----------
01/06/2008For 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:
Running this query would return:
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:
or by adding an additional concatenation operator:
Both methods return the desire result:
A more useful example that also demonstrates the "of the same data type" caveat mentioned above:
would return:
However, if we failed to convert result of getdate() into a varchar, we would receive an error similar to:
For additional information, please visit: http://msdn2.microsoft.com/en-us/library/ms177561.aspx
select 'Windows' + 'Vista' as OSNameRunning this query would return:
OSName
------------
WindowsVistaOK 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 OSNameor by adding an additional concatenation operator:
select 'Windows' + ' ' + 'Vista' as OSNameBoth methods return the desire result:
OSName
-------------
Windows VistaA more useful example that also demonstrates the "of the same data type" caveat mentioned above:
select 'Today is: ' + convert(varchar(10), getdate(), 101) as Todaywould return:
Today
--------------------
Today is: 01/05/2008However, 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
Subscribe to:
Posts (Atom)