<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>นพ ภิญโญวาณิชกะ &#187; MSSQL</title>
	<atom:link href="http://www.nop.in.th/category/database/mssql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nop.in.th</link>
	<description>Nop Pinyowanichaka &#124; ก้าวสั้นๆ ในวันเบาๆ ของผม</description>
	<lastBuildDate>Wed, 14 Jul 2010 01:54:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MSSQL Dateformat การจัดรูปแบบการแสดงผลวันที่ใน MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/mssql-date-format/</link>
		<comments>http://www.nop.in.th/database/mssql/mssql-date-format/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 11:07:36 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=253</guid>
		<description><![CDATA[คราวที่แล้วเขียน SP สร้าง ตารางวันที่ แต่ไอ้ตอนที่แปลง Format มันแปลง ดุ้นๆ ไปไม่ค่อยเวิ๊ก อันนี้เป็นการจัด Format Datetime อย่างเป็นทางการ อันนี้เป็น format นี้ 05-04-2008 SELECT CONVERT&#40;varchar&#40;10&#41;, getdate&#40;&#41;+1, 105&#41; อันนี้เป็น format นี้ 05/04/2008 SELECT CONVERT&#40;varchar&#40;10&#41;, getdate&#40;&#41;+1, 103&#41; ปล. ที่ต้อง +1 ให้กับ getdate() ก็เพราะว่า มันเป็นวันที่ 4 เดือน 4 เดี๋ยวจะงง]]></description>
			<content:encoded><![CDATA[<p>คราวที่แล้วเขียน SP สร้าง ตารางวันที่ แต่ไอ้ตอนที่แปลง Format มันแปลง ดุ้นๆ ไปไม่ค่อยเวิ๊ก อันนี้เป็นการจัด Format Datetime อย่างเป็นทางการ</p>
<p>อันนี้เป็น format นี้ 05-04-2008</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> getdate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">105</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>อันนี้เป็น format นี้ 05/04/2008</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> getdate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">103</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>ปล. ที่ต้อง +1 ให้กับ getdate() ก็เพราะว่า มันเป็นวันที่ 4 เดือน 4 เดี๋ยวจะงง</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/mssql-date-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การเอา Textfile เข้า Temptable ด้วย MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/import-textfile-to-table-with-mssql/</link>
		<comments>http://www.nop.in.th/database/mssql/import-textfile-to-table-with-mssql/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 07:21:57 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=252</guid>
		<description><![CDATA[การเอา Textfile เข้า ฐานข้อมูล MSSQL จริงๆ ไม่ต้องใช้ Temptable ก็ได้ ถ้าใช้บ่อย สร้างเป็นถาวรก็น่าจะใช้งานได้ง่ายกว่า แต่สำหรับผมเองผมว่าเอาเข้า Temptable ไว้ก่อน แล้วค่อยเอามาใช้จริงๆ แต่สำหรับการเขียน Webที่ Connection เปิดหนึ่งครั้ง ปิดหนึ่งครั้ง Temptable ก็หายไปบางครั้งก็คงไม่เหมาะ ขั้นแรกก็สร้าง Temptable ซะก่อน ด้วยคำสั่ง Create table ธรรมดานี่แหละครับ เพียงแต่ใส่ # ไว้ข้างหน้าเพื่อบอกว่าเป็น Temptable 1 2 3 4 5 CREATE TABLE #tempdata ( id varchar&#40;4&#41;, usr VARCHAR&#40;100&#41;, pwd VARCHAR&#40;100&#41; &#41; โครงสร้างไฟล์ text ที่จะเอาเข้าจะเป็นแบบนี้ 1,pixelbar,123 2,pixelable,1234 3,iopixel,123 [...]]]></description>
			<content:encoded><![CDATA[<p>การเอา Textfile เข้า ฐานข้อมูล MSSQL จริงๆ ไม่ต้องใช้ Temptable ก็ได้ ถ้าใช้บ่อย สร้างเป็นถาวรก็น่าจะใช้งานได้ง่ายกว่า แต่สำหรับผมเองผมว่าเอาเข้า Temptable ไว้ก่อน แล้วค่อยเอามาใช้จริงๆ แต่สำหรับการเขียน Webที่ Connection เปิดหนึ่งครั้ง ปิดหนึ่งครั้ง Temptable ก็หายไปบางครั้งก็คงไม่เหมาะ</p>
<p>ขั้นแรกก็สร้าง Temptable ซะก่อน ด้วยคำสั่ง Create table ธรรมดานี่แหละครับ เพียงแต่ใส่ # ไว้ข้างหน้าเพื่อบอกว่าเป็น Temptable</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#tempdata (</span>
id varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
usr VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
pwd VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><span id="more-141"></span></p>
<p>โครงสร้างไฟล์ text ที่จะเอาเข้าจะเป็นแบบนี้</p>
<p>1,pixelbar,123<br />
2,pixelable,1234<br />
3,iopixel,123</p>
<p>ซึ่งจะเรียงก็จะเป็น id,username,password</p>
<p>จากนั้นเราจะเอา text file เข้า สมมุติมันอยู่ใน Drive C ของ Server ชื่อไฟล์ abc.csv</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">BULK <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #808080; font-style: italic;">#tempdata</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'c:<span style="color: #000099; font-weight: bold;">\a</span>bc.csv'</span>
<span style="color: #993333; font-weight: bold;">WITH</span>
<span style="color: #66cc66;">&#40;</span>
FIELDTERMINATOR <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span>
ROWTERMINATOR <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>แค่นี้เราก็สามารถ select ดูข้อมูลได้แล้วครับ จะเอา้ข้อมูลไป Insert ต่อ หรือว่า จะไปปู้ยี่ปู้ยำอะไรก็ได้ สบายๆครับ</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/import-textfile-to-table-with-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>เขียน MSSQL store procedure เพื่อทำ Crosstab หรือ Pivot Table</title>
		<link>http://www.nop.in.th/database/mssql/create-mssql-2000-crosstab/</link>
		<comments>http://www.nop.in.th/database/mssql/create-mssql-2000-crosstab/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 03:56:51 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=251</guid>
		<description><![CDATA[เขียน MSSQL store procedure เพื่อทำ Crosstab หรือ Pivot Table วันก่อน มีโอกาสต้องเขียน การแสดงผลข้อมูลเป็นแบบ แนวนอน หรือที่เค้าเรียกกันว่า Crosstab แต่เนื่องด้วย ฐานข้อมูลที่ใช้ มันเป็น MSSQL 2000 มันไม่มีพวก Pivot Table มาให้ (Access ยังมีเลย) เลยต้องหาเอาเอง ไปเจอเว็บนึงเค้า เขียนเป็น Store Procedure ไว้ เอามาใช้ Work เลยทีเดียว ตอบโจทย์ได้เลย ตัวอย่างของ Data Year Region Income Y2007 North 20 Y2007 South 10 Y2007 East 30 Y2006 North 10 Y2006 South [...]]]></description>
			<content:encoded><![CDATA[<p><strong>เขียน MSSQL store procedure เพื่อทำ Crosstab หรือ Pivot Table</strong></p>
<p>วันก่อน มีโอกาสต้องเขียน การแสดงผลข้อมูลเป็นแบบ แนวนอน หรือที่เค้าเรียกกันว่า Crosstab แต่เนื่องด้วย ฐานข้อมูลที่ใช้ มันเป็น MSSQL 2000 มันไม่มีพวก Pivot Table มาให้ (Access ยังมีเลย) เลยต้องหาเอาเอง ไปเจอเว็บนึงเค้า เขียนเป็น Store Procedure ไว้ เอามาใช้ Work เลยทีเดียว ตอบโจทย์ได้เลย</p>
<p>ตัวอย่างของ Data</p>
<p><strong>Year Region Income</strong><br />
Y2007 North 20<br />
Y2007 South 10<br />
Y2007 East 30<br />
Y2006 North 10</p>
<p>Y2006 South 10</p>
<p>Y2006 East 10</p>
<p>เมื่อทำ Crosstab แล้วจะได้แบบนี้<br />
ํ<br />
<strong>Year North South East</strong><br />
Y2006 10 10 10<br />
Y2007 20 10 30</p>
<p>อ้าวแล้วทำยังไงให้ได้แบบนี้ล่ะ มาดูวิธีการกันเลยครับ</p>
<p><span id="more-140"></span></p>
<p><strong>เขียน Store Proc ดังนี้</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span>     PROC sp_CrossTab
  @<span style="color: #993333; font-weight: bold;">TABLE</span>       <span style="color: #993333; font-weight: bold;">AS</span> sysname<span style="color: #66cc66;">,</span>        <span style="color: #808080; font-style: italic;">-- Table to crosstab</span>
  @onrows      <span style="color: #993333; font-weight: bold;">AS</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>  <span style="color: #808080; font-style: italic;">-- Grouping key values (on rows)</span>
  @onrowsalias <span style="color: #993333; font-weight: bold;">AS</span> sysname <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> <span style="color: #808080; font-style: italic;">-- Alias for grouping column</span>
  @oncols      <span style="color: #993333; font-weight: bold;">AS</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>  <span style="color: #808080; font-style: italic;">-- Destination columns (on columns)</span>
  @sumcol      <span style="color: #993333; font-weight: bold;">AS</span> sysname <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span>  <span style="color: #808080; font-style: italic;">-- Data cells</span>
<span style="color: #993333; font-weight: bold;">AS</span>
&nbsp;
DECLARE
  @sql <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  @NEWLINE <span style="color: #993333; font-weight: bold;">AS</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @NEWLINE <span style="color: #66cc66;">=</span> CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- step 1: beginning of SQL string</span>
<span style="color: #993333; font-weight: bold;">SET</span> @sql <span style="color: #66cc66;">=</span>
  <span style="color: #ff0000;">'SELECT'</span> <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'  '</span> <span style="color: #66cc66;">+</span> @onrows <span style="color: #66cc66;">+</span>
  CASE
    WHEN @onrowsalias <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #ff0000;">' AS '</span> <span style="color: #66cc66;">+</span> @onrowsalias
    ELSE <span style="color: #ff0000;">''</span>
  END
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#keys(keyvalue nvarchar(100) NOT NULL PRIMARY KEY)</span>
&nbsp;
DECLARE @keyssql <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SET</span> @keyssql <span style="color: #66cc66;">=</span>
  <span style="color: #ff0000;">'INSERT INTO #keys '</span> <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'SELECT DISTINCT CAST('</span> <span style="color: #66cc66;">+</span> @oncols <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' AS nvarchar(100)) '</span> <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'FROM '</span> <span style="color: #66cc66;">+</span> @<span style="color: #993333; font-weight: bold;">TABLE</span>
&nbsp;
EXEC <span style="color: #66cc66;">&#40;</span>@keyssql<span style="color: #66cc66;">&#41;</span>
&nbsp;
DECLARE @<span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AS</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">=</span> MIN<span style="color: #66cc66;">&#40;</span>keyvalue<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#keys</span>
&nbsp;
WHILE @<span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
BEGIN
  <span style="color: #993333; font-weight: bold;">SET</span> @sql <span style="color: #66cc66;">=</span> @sql <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">','</span>                   <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
    <span style="color: #ff0000;">'  SUM(CASE CAST('</span> <span style="color: #66cc66;">+</span> @oncols <span style="color: #66cc66;">+</span>
                     <span style="color: #ff0000;">' AS nvarchar(100))'</span> <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
    <span style="color: #ff0000;">'        WHEN N'</span><span style="color: #ff0000;">''</span> <span style="color: #66cc66;">+</span> @<span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">+</span>
           <span style="color: #ff0000;">''</span><span style="color: #ff0000;">' THEN '</span> <span style="color: #66cc66;">+</span> CASE
                          WHEN @sumcol <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span> THEN <span style="color: #ff0000;">'1'</span>
                          ELSE @sumcol
                        END <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
    <span style="color: #ff0000;">'        ELSE 0'</span>                      <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
    <span style="color: #ff0000;">'      END) AS D'</span> <span style="color: #66cc66;">+</span> @<span style="color: #993333; font-weight: bold;">KEY</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">=</span> MIN<span style="color: #66cc66;">&#40;</span>keyvalue<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#keys</span>
  <span style="color: #993333; font-weight: bold;">WHERE</span> keyvalue &amp;amp;gt; @<span style="color: #993333; font-weight: bold;">KEY</span>
END
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @sql <span style="color: #66cc66;">=</span> @sql         <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'FROM '</span> <span style="color: #66cc66;">+</span> @<span style="color: #993333; font-weight: bold;">TABLE</span>      <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'GROUP BY '</span> <span style="color: #66cc66;">+</span> @onrows <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #66cc66;">+</span>
  <span style="color: #ff0000;">'ORDER BY '</span> <span style="color: #66cc66;">+</span> @onrows
&nbsp;
 PRINT @sql  <span style="color: #66cc66;">+</span> @NEWLINE <span style="color: #808080; font-style: italic;">-- For debug</span>
EXEC <span style="color: #66cc66;">&#40;</span>@sql<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>ใน Database Master (เพื่อเรียกใ้ช้ได้ทุก Database แบบง่ายๆหน่อย)</p>
<p>วิธีเรียกใช้งานก็</p>
<p>EXEC sp_crosstab<br />
@table = &#8216;ชื่อ Table ที่ต้องการสร้าง Crosstab จากมัน&#8217;,<br />
@onrows = &#8216;Field ที่ต้องการเอามาเป็น Row&#8217;,<br />
@oncols = &#8216;Field ที่ต้องการเอามาเป็น Column&#8217;,<br />
@sumcol = &#8216;Field ที่ต้องการเอามาเป็น จำนวนรวม&#8217;</p>
<p>แค่นี้เราก็สามารถสร้าง ตาราง Crosstab ได้อย่างง่ายๆ แล้วครับ</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/create-mssql-2000-crosstab/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>การสร้างฐานข้อมูลวันที่ใน Table ด้วย MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/create-date-table-with-mssql/</link>
		<comments>http://www.nop.in.th/database/mssql/create-date-table-with-mssql/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 06:45:26 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=250</guid>
		<description><![CDATA[วันนี้มีความต้องการสร้างฐานข้อมูลวันที่ในตารางของ MSSQL แต่ให้ไปนั่ง Key เอาหรือไป Copy เอาคงไม่ไหว เลยเขียน Script เอาไว้สร้างวันที่เอาไว้เผื่อใครเอาไปใช้ครับ declare @i int declare @CAL_DATE datetime SET @i=0 while &#40;@i &#38;lt; 1825&#41; begin -- เอาวันพรุ่งนี้เข้า SET @CAL_DATE = &#40;SELECT REPLACE&#40;CONVERT&#40;varchar&#40;11&#41;, GETDATE&#40;&#41;+@i, 111&#41;,'/','-'&#41;&#41; INSERT INTO MS_CALENDAR &#40;CAL_DATE&#41; VALUES &#40;@CAL_DATE&#41; -- เอาวันพรุ่งนี้เข้า -- เอาเมื่อวานเข้า SET @CAL_DATE = &#40;SELECT REPLACE&#40;CONVERT&#40;varchar&#40;11&#41;, GETDATE&#40;&#41;-@i, 111&#41;,'/','-'&#41;&#41; INSERT INTO MS_CALENDAR &#40;CAL_DATE&#41; VALUES &#40;@CAL_DATE&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>วันนี้มีความต้องการสร้างฐานข้อมูลวันที่ในตารางของ MSSQL แต่ให้ไปนั่ง Key เอาหรือไป Copy เอาคงไม่ไหว เลยเขียน Script เอาไว้สร้างวันที่เอาไว้เผื่อใครเอาไปใช้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">declare @i int
declare  @CAL_DATE datetime
<span style="color: #993333; font-weight: bold;">SET</span> @i<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>
while <span style="color: #66cc66;">&#40;</span>@i &amp;lt; <span style="color: #cc66cc;">1825</span><span style="color: #66cc66;">&#41;</span>
begin
	<span style="color: #808080; font-style: italic;">-- เอาวันพรุ่งนี้เข้า</span>
	<span style="color: #993333; font-weight: bold;">SET</span> @CAL_DATE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span>@i<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">111</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> MS_CALENDAR <span style="color: #66cc66;">&#40;</span>CAL_DATE<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>@CAL_DATE<span style="color: #66cc66;">&#41;</span>
	<span style="color: #808080; font-style: italic;">-- เอาวันพรุ่งนี้เข้า</span>
	<span style="color: #808080; font-style: italic;">-- เอาเมื่อวานเข้า</span>
	<span style="color: #993333; font-weight: bold;">SET</span> @CAL_DATE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">-</span>@i<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">111</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> MS_CALENDAR <span style="color: #66cc66;">&#40;</span>CAL_DATE<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>@CAL_DATE<span style="color: #66cc66;">&#41;</span>
	<span style="color: #808080; font-style: italic;">-- เอาเมื่อวานเข้า</span>
	<span style="color: #993333; font-weight: bold;">SET</span> @i<span style="color: #66cc66;">=</span>@i<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>
end</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/create-date-table-with-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การ Add Server Name เพื่อทำ Link Server ใน MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/add-server-name-for-link-server-in-mssql/</link>
		<comments>http://www.nop.in.th/database/mssql/add-server-name-for-link-server-in-mssql/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 06:07:43 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=232</guid>
		<description><![CDATA[ทำการ Add Server ด้วย System Store Procedure ก่อน ด้วยคำสั่ง use Master EXEC sp_addlinkedserver    @server = servername ,    @provider = &#8216;MSDASQL&#8217;,    @provstr = &#8216;DRIVER={SQL Server};SERVER=servername;UID=user;PWD=password;&#8217; หลังจากนั้น ก็รัน SP เพื่อ ปรับ Config Data Access EXEC sp_serveroption &#8217;178.55.1.3&#8242;, &#8216;data access&#8217;, &#8216;TRUE&#8217; ถ้าไม่รันคำสั่งนี้ จะขึ้น Error อันนี้ servername is not configured for DATA ACCESS. เท่านี้เราก็สามารถเรียก Table ของ [...]]]></description>
			<content:encoded><![CDATA[<p><strong>ทำการ Add Server ด้วย System Store Procedure ก่อน ด้วยคำสั่ง</strong></p>
<p>use Master</p>
<p>EXEC sp_addlinkedserver<br />
   @server = servername ,<br />
   @provider = &#8216;MSDASQL&#8217;,<br />
   @provstr = &#8216;DRIVER={SQL Server};SERVER=servername;UID=user;PWD=password;&#8217;</p>
<p>หลังจากนั้น ก็รัน SP เพื่อ ปรับ Config Data Access</p>
<p>EXEC sp_serveroption &#8217;178.55.1.3&#8242;, &#8216;data access&#8217;, &#8216;TRUE&#8217;</p>
<p>ถ้าไม่รันคำสั่งนี้ จะขึ้น Error อันนี้</p>
<p>servername is not configured for DATA ACCESS.</p>
<p>เท่านี้เราก็สามารถเรียก Table ของ MSSQL ข้าม Server ได้แล้ว</p>
<p>เช่นถ้าต้องการเรียก Table ABC บน Database ADatabase ที่อยู่ใน AServer ก้อจะเรียกดังนี้</p>
<p>select * from [Aserver].ADatabase.dbo.ABC เป็นต้น</p>
<p class="poweredbyperformancing">Powered by <a href="http://scribefire.com/">ScribeFire</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/add-server-name-for-link-server-in-mssql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Script สร้าง Datadict สำหรับ MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/script-%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-datadict-%e0%b8%aa%e0%b8%b3%e0%b8%ab%e0%b8%a3%e0%b8%b1%e0%b8%9a-mssql/</link>
		<comments>http://www.nop.in.th/database/mssql/script-%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-datadict-%e0%b8%aa%e0%b8%b3%e0%b8%ab%e0%b8%a3%e0%b8%b1%e0%b8%9a-mssql/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 09:11:24 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=198</guid>
		<description><![CDATA[Script สำหรับสร้าง Datadict สำหรับ MSSQL select&#160; b.colid as [ID] , a.NAME as TABLE_NAME&#160; , b.name&#160; as COLUMN_NAME ,&#160;&#160; c.name&#160; as [TYPE], b.length as SIZE from sysobjects a,syscolumns b,systypes c where a.id = b.id&#160; and a.xtype = 'U' and a.name in ('MS_TRANSPORT_MODEL') and b.xusertype = c.xusertype]]></description>
			<content:encoded><![CDATA[<p>Script สำหรับสร้าง Datadict สำหรับ MSSQL</p>
<pre lang=sql line=1>
select&nbsp; b.colid as [ID] , a.NAME as TABLE_NAME&nbsp; , b.name&nbsp; as COLUMN_NAME ,&nbsp;&nbsp; c.name&nbsp; as [TYPE], b.length as SIZE
from sysobjects a,syscolumns b,systypes c
where a.id = b.id&nbsp; and a.xtype = 'U' and a.name in ('MS_TRANSPORT_MODEL') and
b.xusertype = c.xusertype
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/script-%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-datadict-%e0%b8%aa%e0%b8%b3%e0%b8%ab%e0%b8%a3%e0%b8%b1%e0%b8%9a-mssql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>MSSQL Error (Could not allocate ancillary table &#8230;)</title>
		<link>http://www.nop.in.th/database/mssql/mssql-errror-could-not-allocate-ancillary-table/</link>
		<comments>http://www.nop.in.th/database/mssql/mssql-errror-could-not-allocate-ancillary-table/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 04:59:12 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=197</guid>
		<description><![CDATA[Could not allocate ancillary table for view or function resolution. The maximum number of tables in a query (260) was exceeded. เหตุเกิดที่ว่าผมต้องสร้าง View เพื่อทำการตรวจสอบผลการทำงานทั้งหมดของระบบ เนื่องจากต้องไปวิ่งไล่ Table แทบทั้ง Database เพื่อทำการตรวจสอบความถูกต้องของข้อมูล ปรากฏว่ามัน Join กันเยอะเกินไป จากหลายๆ Table มันเลย แจ้งว่า The maximum number of tables in a query (260) was exceeded. คือ เลือก Table มามากเกินไปใน Query วิธีแก้ไขของผมคือไปสร้าง Function มาแทน [...]]]></description>
			<content:encoded><![CDATA[<p>Could not allocate ancillary table for view or function resolution. The maximum number of tables in a query (260) was exceeded.</p>
<p>เหตุเกิดที่ว่าผมต้องสร้าง View เพื่อทำการตรวจสอบผลการทำงานทั้งหมดของระบบ เนื่องจากต้องไปวิ่งไล่ Table แทบทั้ง Database เพื่อทำการตรวจสอบความถูกต้องของข้อมูล ปรากฏว่ามัน Join กันเยอะเกินไป จากหลายๆ Table มันเลย แจ้งว่า The maximum number of tables in a query (260) was exceeded. คือ เลือก Table มามากเกินไปใน Query วิธีแก้ไขของผมคือไปสร้าง Function มาแทน แล้วใน View นี้ไปเรียก Function ที่ไปเรื่อง Table เยอะๆ อีกทีนึง (จริงๆมันก็ช้านะครับ แต่ผมรับได้กับการตรวจสอบความถูกต้องของข้อมูลก่อนนำเข้าระบบหลักแบบนี้)</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/mssql-errror-could-not-allocate-ancillary-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>หน่วงเวลาใน MSSQL (MSSQL Delay)</title>
		<link>http://www.nop.in.th/database/mssql/%e0%b8%ab%e0%b8%99%e0%b9%88%e0%b8%a7%e0%b8%87%e0%b9%80%e0%b8%a7%e0%b8%a5%e0%b8%b2%e0%b9%83%e0%b8%99-mssql-mssql-delay/</link>
		<comments>http://www.nop.in.th/database/mssql/%e0%b8%ab%e0%b8%99%e0%b9%88%e0%b8%a7%e0%b8%87%e0%b9%80%e0%b8%a7%e0%b8%a5%e0%b8%b2%e0%b9%83%e0%b8%99-mssql-mssql-delay/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 06:32:21 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=196</guid>
		<description><![CDATA[ไม่ได้เขียน Blog เสียนาน เพราะไม่ค่อยมีเรื่องอะไรใหม่ๆ ในชีวิต แล้วก็หนีไปบวชมา เลยไม่ได้เขียนมาซะนาน วันนี้ได้โจทย์ว่าต้อง Delay การแสดงผล หรือ การอัพเดตข้อมูลชุด หนึ่ง ก่อนอีกชุดนึงมา ลองค้นๆ MSSQL มี คำสั่งในการชะลอการทำงาน ดังนี้ waitfor delay &#8217;00:00:01&#8242; อันนี้คือ รอ 1 วินาที ตัวอย่าง select getdate()waitfor delay &#8217;00:00:02&#8242; select getdate() ผลลัพท์ 2007-12-11 13:26:19.1632007-12-11 13:26:21.180 ต่างกัน 2 วินาที เสร็จครับ]]></description>
			<content:encoded><![CDATA[<p>ไม่ได้เขียน Blog เสียนาน เพราะไม่ค่อยมีเรื่องอะไรใหม่ๆ ในชีวิต แล้วก็หนีไปบวชมา เลยไม่ได้เขียนมาซะนาน วันนี้ได้โจทย์ว่าต้อง Delay การแสดงผล หรือ การอัพเดตข้อมูลชุด หนึ่ง ก่อนอีกชุดนึงมา ลองค้นๆ MSSQL มี คำสั่งในการชะลอการทำงาน ดังนี้</p>
<p>waitfor delay &#8217;00:00:01&#8242; </p>
<p>อันนี้คือ รอ 1 วินาที</p>
<p>ตัวอย่าง</p>
<p>select getdate()<br />waitfor delay &#8217;00:00:02&#8242; <br />select getdate()</p>
<p>ผลลัพท์</p>
<p>2007-12-11 13:26:19.163<br />2007-12-11 13:26:21.180</p>
<p>ต่างกัน 2 วินาที</p>
<p>เสร็จครับ</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/%e0%b8%ab%e0%b8%99%e0%b9%88%e0%b8%a7%e0%b8%87%e0%b9%80%e0%b8%a7%e0%b8%a5%e0%b8%b2%e0%b9%83%e0%b8%99-mssql-mssql-delay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hex To Decimal and Decimal to Hex in MSSQL</title>
		<link>http://www.nop.in.th/database/mssql/hex-to-decimal-and-decimal-to-hex-in-mssql/</link>
		<comments>http://www.nop.in.th/database/mssql/hex-to-decimal-and-decimal-to-hex-in-mssql/#comments</comments>
		<pubDate>Fri, 20 Jul 2007 05:04:46 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=151</guid>
		<description><![CDATA[ไม่ได้เขียนมาซะนานเนื่องจากงานยุ่ง วันก่อนมีพี่คนนึงถามว่า &#8220;เอ๊ะ ใน MSSQL มี Function Hex To Decimal และ Decimal to Hex มั้ย?&#8221; ผมก็เอาเลยเริ่มค้นหา ก็ทำให้รู้ว่ามันไม่มี Function นี้ในตัวของมันเอง ต้องเขียนเอง เลยลองค้นๆใน Google ดู ก็พบ Function ที่ Work!! ลองเอาไปใช้งานกันดูครับ Function IntToHex 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [...]]]></description>
			<content:encoded><![CDATA[<p>ไม่ได้เขียนมาซะนานเนื่องจากงานยุ่ง วันก่อนมีพี่คนนึงถามว่า</p>
<p>&#8220;เอ๊ะ ใน MSSQL มี Function Hex To Decimal และ Decimal to Hex มั้ย?&#8221;</p>
<p>ผมก็เอาเลยเริ่มค้นหา ก็ทำให้รู้ว่ามันไม่มี Function นี้ในตัวของมันเอง ต้องเขียนเอง เลยลองค้นๆใน Google ดู ก็พบ Function ที่ Work!! ลองเอาไปใช้งานกันดูครับ</p>
<p><span id="more-95"></span></p>
<h3>Function IntToHex</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> HexToInt<span style="color: #66cc66;">&#40;</span>@vsData char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
RETURNS int <span style="color: #993333; font-weight: bold;">AS</span>
&nbsp;
begin
&nbsp;
 declare @iDataLength int
&nbsp;
 declare @iDataLengthM1 int
&nbsp;
 declare @iResult     int <span style="color: #993333; font-weight: bold;">SET</span> @iResult <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
 declare @i           int <span style="color: #993333; font-weight: bold;">SET</span> @i       <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
 declare @iTemp       int <span style="color: #993333; font-weight: bold;">SET</span> @iTemp   <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
 declare @iTempMax    int
&nbsp;
 <span style="color: #993333; font-weight: bold;">SET</span> @iDataLength<span style="color: #66cc66;">=</span>len<span style="color: #66cc66;">&#40;</span>@vsData<span style="color: #66cc66;">&#41;</span>
&nbsp;
 <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> @vsData <span style="color: #993333; font-weight: bold;">LIKE</span> replicate<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9a-fA-F]'</span><span style="color: #66cc66;">,</span>@iDataLength<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NULL</span>
&nbsp;
 <span style="color: #993333; font-weight: bold;">SET</span> @iDataLengthM1<span style="color: #66cc66;">=</span>@iDataLength<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>
&nbsp;
 while @i &amp;lt; @iDataLength begin
&nbsp;
  <span style="color: #993333; font-weight: bold;">IF</span> @i<span style="color: #66cc66;">=</span>@iDataLengthM1 begin
&nbsp;
   <span style="color: #993333; font-weight: bold;">SET</span> @iTempMax<span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#40;</span>ASCII<span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>@vsData<span style="color: #66cc66;">,</span> @iDataLength <span style="color: #66cc66;">-</span> @i<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>&amp;amp;<span style="color: #cc66cc;">79</span><span style="color: #66cc66;">&#41;</span>%55
&nbsp;
   <span style="color: #993333; font-weight: bold;">SET</span> @iResult <span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#40;</span>@iResult<span style="color: #66cc66;">+</span><span style="color: #66cc66;">&#40;</span>@iTempMax&amp;amp;<span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>@iTemp<span style="color: #66cc66;">&#41;</span>
&nbsp;
   <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #66cc66;">&#40;</span>@iTempMax&amp;amp;<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">8</span> <span style="color: #993333; font-weight: bold;">SET</span> @iResult<span style="color: #66cc66;">=</span>@iResult<span style="color: #66cc66;">-</span><span style="color: #66cc66;">&#40;</span>Power<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">,</span>@iDataLengthM1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">8</span>
&nbsp;
  end else begin
&nbsp;
   <span style="color: #993333; font-weight: bold;">SET</span> @iResult <span style="color: #66cc66;">=</span>@iResult<span style="color: #66cc66;">+</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ASCII<span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>@vsData<span style="color: #66cc66;">,</span> @iDataLength <span style="color: #66cc66;">-</span> @i<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>&amp;amp;<span style="color: #cc66cc;">79</span><span style="color: #66cc66;">&#41;</span>%55<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>@iTemp
&nbsp;
  end
&nbsp;
  <span style="color: #993333; font-weight: bold;">SET</span> @i <span style="color: #66cc66;">=</span> @i <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">IF</span> @i&amp;lt;<span style="color: #cc66cc;">8</span> <span style="color: #993333; font-weight: bold;">SET</span> @iTemp <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span> <span style="color: #66cc66;">*</span> @iTemp
&nbsp;
 end
&nbsp;
 <span style="color: #993333; font-weight: bold;">RETURN</span> @iResult
&nbsp;
end</pre></td></tr></table></div>

<h3>Function HexToInt</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>IntToHex<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>@Dec <span style="color: #993333; font-weight: bold;">AS</span> bigint<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--@Hex As varchar(8) Output)</span>
&nbsp;
returns varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>
&nbsp;
BEGIN
&nbsp;
      Declare @HexDigits <span style="color: #993333; font-weight: bold;">AS</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      declare @Hex <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #993333; font-weight: bold;">SET</span> @HexDigits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'0123456789ABCDEF'</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @Hex <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span>
&nbsp;
      While @Dec &amp;gt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span>
&nbsp;
         Begin
&nbsp;
           <span style="color: #993333; font-weight: bold;">SET</span> @Hex <span style="color: #66cc66;">=</span> SubString<span style="color: #66cc66;">&#40;</span>@HexDigits<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>@Dec % <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> @Hex
&nbsp;
           <span style="color: #993333; font-weight: bold;">SET</span> @Dec <span style="color: #66cc66;">=</span> @Dec <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">16</span>
&nbsp;
         End
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @Hex <span style="color: #66cc66;">=</span> SubString<span style="color: #66cc66;">&#40;</span>@HexDigits<span style="color: #66cc66;">,</span> @Dec <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> @Hex
&nbsp;
<span style="color: #993333; font-weight: bold;">RETURN</span> @hex
&nbsp;
end</pre></td></tr></table></div>

<p><strong>ตัวอย่างการเรียกใช้งาน</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>HexToInt<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2A'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> HexToInt<span style="color: #66cc66;">,</span> dbo<span style="color: #66cc66;">.</span>IntToHex<span style="color: #66cc66;">&#40;</span>dbo<span style="color: #66cc66;">.</span>HexToInt<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2A'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> IntToHex</pre></td></tr></table></div>

<p><strong>result</strong><br />
42	2A</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/hex-to-decimal-and-decimal-to-hex-in-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MSSQL Split Function</title>
		<link>http://www.nop.in.th/database/mssql/mssql-split-function/</link>
		<comments>http://www.nop.in.th/database/mssql/mssql-split-function/#comments</comments>
		<pubDate>Thu, 28 Jun 2007 04:41:59 +0000</pubDate>
		<dc:creator>นพ</dc:creator>
				<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://www.nop.in.th/?p=128</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 CREATE FUNCTION dbo.Split&#40;@String nvarchar&#40;4000&#41;, [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> dbo<span style="color: #66cc66;">.</span>Split<span style="color: #66cc66;">&#40;</span>@String nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @Delimiter char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
RETURNS @Results <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span>Items nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">AS</span>    BEGIN
&nbsp;
    DECLARE @<span style="color: #993333; font-weight: bold;">INDEX</span> INT
&nbsp;
    DECLARE @SLICE nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">--     ERO FIRST TIME IN LOOP</span>
&nbsp;
    <span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
    WHILE @<span style="color: #993333; font-weight: bold;">INDEX</span> !<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>
&nbsp;
BEGIN
&nbsp;
              <span style="color: #808080; font-style: italic;">-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER</span>
&nbsp;
              <span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #66cc66;">=</span> CHARINDEX<span style="color: #66cc66;">&#40;</span>@Delimiter<span style="color: #66cc66;">,</span>@STRING<span style="color: #66cc66;">&#41;</span>
&nbsp;
              <span style="color: #808080; font-style: italic;">-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE</span>
&nbsp;
              <span style="color: #993333; font-weight: bold;">IF</span> @<span style="color: #993333; font-weight: bold;">INDEX</span> !<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>
&nbsp;
                    <span style="color: #993333; font-weight: bold;">SELECT</span> @SLICE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>@STRING<span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
              ELSE
&nbsp;
                    <span style="color: #993333; font-weight: bold;">SELECT</span> @SLICE <span style="color: #66cc66;">=</span> @STRING
&nbsp;
              <span style="color: #808080; font-style: italic;">-- PUT THE ITEM INTO THE RESULTS SET</span>
&nbsp;
              <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @Results<span style="color: #66cc66;">&#40;</span>Items<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>@SLICE<span style="color: #66cc66;">&#41;</span>
&nbsp;
              <span style="color: #808080; font-style: italic;">-- CHOP THE ITEM REMOVED OFF THE MAIN STRING</span>
&nbsp;
              <span style="color: #993333; font-weight: bold;">SELECT</span> @STRING <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>@STRING<span style="color: #66cc66;">,</span>LEN<span style="color: #66cc66;">&#40;</span>@STRING<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> @<span style="color: #993333; font-weight: bold;">INDEX</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
              <span style="color: #808080; font-style: italic;">-- BREAK OUT IF WE ARE DONE</span>
&nbsp;
              <span style="color: #993333; font-weight: bold;">IF</span> LEN<span style="color: #66cc66;">&#40;</span>@STRING<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> BREAK
&nbsp;
    END
&nbsp;
    <span style="color: #993333; font-weight: bold;">RETURN</span>
&nbsp;
END</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nop.in.th/database/mssql/mssql-split-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
