Post by Goborijung at 2019-12-11 14:53:12 | ID: 305
ref : https://saixiii.com/database-sql-data-types/ Converting Data Type Functions เป็นฟังก์ชั่นสำหรับแปลงข้อมูลต่างชนิดกัน ให้อยู่ในรูปแบบที่ใช้งานด้วยกันได้ Style ที่จำเป็นต้องใช้สำหรับข้อมูลประเภท Datetime เพื่อแปลงให้เป็น Character /* แปลง int ให้เป็น varchar */ declare @age as int = 20 select CAST(@age as varchar(4)) /* แปลง Date ให้อยู่ในรูปแบบ Style ที่ต้องการ */ /* 103 dd/mm/yy 105 dd-mm-yy 108 hh:mm:ss 111 yy/mm/dd */ select getdate() select convert(varchar(10),GETDATE(),103) AS cdate103 /* ตรวจสอบว่ารูปแบบนั้นเป็นวันที่หรือไม่ Y?1:0 */ select ISDATE('2000-11-11') select ISDATE('1000-11-11') /* ตรวจสอบว่ารูปแบบนั้นเป็นตัวเลขหรือไม่ Y?1:0 */ select ISNUMERIC('2533') select ISNUMERIC('2533s')
Post by Goborijung at 2019-12-10 14:15:39 | ID: 270
COUNT นับจำนวนแถวของกลุ่มข้อมูล Ex. SELECT CONo ,COUNT(ORDQTYPCS) AS count_ordqty FROM TM_SO GROUP BY CONo ORDER BY CONo Ex. SELECT CONo ,COUNT(DISTINCT ORDQTYPCS) AS count_ordqty FROM TM_SO GROUP BY CONo ORDER BY CONo
Post by Goborijung at 2019-12-06 09:44:31 | ID: 211
การสร้างแบบปกติ
CREATE TABLE table_name ( OID int NOT NULL, Name nvarchar(50), Address nvarchar(100), Tel nvarchar(20) )การสร้างแบบกำหนด Primary Key ด้วย
CREATE TABLE table_name ( OID int NOT NULL PRIMARY KEY, Name nvarchar(50), Address nvarchar(100), Tel nvarchar(20) )การสร้างแบบกำหนด Foreign Key ด้วย
Create Table H_ORDER ( Order_no int default 0 not null Primary key, Cus_id char(5) default '' not null, Order_Date datetime default '2000-01-01' not null, Foreign key (Cus_id) References CUS_NAME(Cus_id) )
Post by Goborijung at 2019-12-20 10:32:55 | ID: 347
https://bit.ly/2Q6Momu Cross Apply นั้นเป็น function เฉพาะของ MS SQL Server ซึ่งหลายคนอาจจะยังไม่เคยใช้ Statement นี้ ซึ่ง Cross Apply จะอนุญาตให้เรียบเรียงข้อมูลของ sub table โดยใช้ key ของ Table หลักเป็นเงื่อนไขในการ where clause โดยที่ sub table ยังสามารถใช้ Top หรือ Order by เพื่อสร้างรูปแบบข้อมูลที่เรียกว่า first from many row selection/retrieval ได้ OUTER APPLY มีรูปแบบเหมือนกับ LEFT JOIN ซึ่งอธิบายง่ายๆ คือหากไม่มีข้อมูลใน sub Table ก็จะแสดงค่า NULL ออกมา ** CROSS APPLY จะให้ค่าเหมือนกันกับ INNER JOIN Ex. select top 2 * from Department d inner join [User] u on u.Department = d.OID select top 2 * from Department d cross apply(select * from [User] u where u.Department = d.OID) u ** OUTER APPLY จะให้ค่าเหมือนกันกับ LEFT JOIN Ex. select top 2 * from Department d left join [User] u on u.Department = d.OID select top 2 * from Department d left outer join [User] u on u.Department = d.OID select top 2 * from Department d outer apply(select * from [User] u where u.Department = d.OID) u
Post by Goborijung at 2019-12-10 10:29:47 | ID: 262
CROSS JOIN เป็นการเชื่อมตารางตั้งแต่ 2 ตารางขึ้นไป โดยนำทุกๆแถวของตารางหนึ่ง มาเชื่อมกับข้อมูลทุกๆแถวของอีกตารางหนึ่ง ซึ่ง จำนวนแถวที่ได้ จะเท่ากับผลคูณของตารางที่นำมาเชื่อมกัน Ex. SELECT * FROM Section AS s CROSS JOIN [User] AS u WHERE u.NickName IS NOT NULL ORDER BY s.OID
Post by Goborijung at 2019-12-11 11:03:42 | ID: 297
Date Function เป็นฟังก์ชั่นที่ใช้ในการจัดการเกี่ยวกับวันที่และเวลา ประกอบไปด้วยฟังก์ชั่นต่างๆดังนี้ /* get date */ select getdate() as getdates /* get day */ select day(getdate()) as dates /* add date 1 day */ select dateadd(day,1,getdate()) as date_add /* add date 1 month */ select dateadd(month,1,getdate()) as month_add /* add date 1 year */ select dateadd(year,1,getdate()) as year_add /* หาจำนวนวันที่ห่างกันของวันเวลา (วัน) */ select datediff(day,'2018-12-11','2019-12-11') as day_diff /* หาจำนวนวันที่ห่างกันของวันเวลา (เดือน) */ select datediff(month,'2018-12-11','2019-12-11') as month_diff /* หาจำนวนวันที่ห่างกันของวันเวลา (ปี) */ select datediff(year,'2018-12-11','2019-12-11') as year_diff /* แสดงชื่อ วัน */ select datename(day,GETDATE()) as day_name /* แสดงชื่อ เดือน */ select datename(month,GETDATE()) as month_name /* แสดงชื่อ ปี */ select datename(year,GETDATE()) as year_name /* get day */ select datepart(day,GETDATE()) as day_part /* get month */ select datepart(month,GETDATE()) as month_part /* get day */ select datepart(year,GETDATE()) as year_part /* get day */ select day('2019-12-11') as dayss /* get month */ select month('2019-12-11') as months /* get year */ select year('2019-12-11') as years
Post by Goborijung at 2019-12-12 08:29:06 | ID: 309
Syntax : DATEPART(datepart,date) DatePart ตัวย่อ year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww weekday dw, w hour hh minute mi, n second ss, s millisecond ms microsecond mcs nanosecond ns Ex. /* get day */ select datepart(day,GETDATE()) as day_part /* get month */ select datepart(month,GETDATE()) as month_part /* get day */ select datepart(year,GETDATE()) as year_part
Post by Goborijung at 2020-01-13 08:42:18 | ID: 364
https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/ Ex. select convert(varchar, getdate(), 23) //2020-01-10 select PODate,left(convert(datetime,PODate,109),11) AS CDate1109 from TM_PO //2019-01-04 00:00:00.000 Jan 4 2019
Post by Goborijung at 2019-12-06 10:33:32 | ID: 215
ALTER TABLE table_name DROP COLUMN column_name Ex. ALTER TABLE test DROP COLUMN Tels
Post by Goborijung at 2019-12-06 11:50:02 | ID: 218
การ Drop Primary Key จะต้อง Drop จาก CONSTRAINT Name ดังนี้ แบบที่ 1 กรณีที่ Generate CONSTRAINT Name ALTER TABLE table_name DROP CONSTRAINT PK__test__keygenerater Ex. ALTER TABLE test DROP CONSTRAINT PK__test__CB394B39CCA8E721 แบบที่ 2 กรณี่ที่มีการกำหนด CONSTRAINT Name เอง ALTER TABLE table_name DROP CONSTRAINT PK_column_name Ex. ALTER TABLE test DROP CONSTRAINT PK_OID ** ถ้าเป็นกรณีที่ 1 ให้ดู CONSTRAINT Name ได้จากคำสั่งนี้ EXEC sp_help table_name