Select id, name from A where name<>lower(name) collate SQL_Latin1_General_CP1_CS_AS
Select id, A.name, B.name from A inner join B on A.name=B.name where A.name<>B.name collate SQL_Latin1_General_CP1_CS_AS
http://www.javashuo.com/article/p-tcpctorc-bc.htmlmysql
國家(country) 人口(population)
中國 600
美國 100
加拿大 100
英國 200
法國 300
日本 250
德國 200
墨西哥 50
印度 250sql
Result:express
洲 人口
亞洲 1100
北美洲 250
其餘 700session
SELECT SUM(population), CASE country WHEN '中國' THEN '亞洲' WHEN '印度' THEN '亞洲' WHEN '日本' THEN '亞洲' WHEN '美國' THEN '北美洲' WHEN '加拿大' THEN '北美洲' WHEN '墨西哥' THEN '北美洲' ELSE '其餘' END FROM Table_A GROUP BY CASE country WHEN '中國' THEN '亞洲' WHEN '印度' THEN '亞洲' WHEN '日本' THEN '亞洲' WHEN '美國' THEN '北美洲' WHEN '加拿大' THEN '北美洲' WHEN '墨西哥' THEN '北美洲' ELSE '其餘' END;
SELECT CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' WHEN salary > 800 AND salary <= 1000 THEN '4' ELSE NULL END salary_class, COUNT(*) FROM Table_A GROUP BY CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' WHEN salary > 800 AND salary <= 1000 THEN '4' ELSE NULL END;
國家(country) 性別(sex) 人口(population)
中國 1 340
中國 2 260
美國 1 45
美國 2 55
加拿大 1 51
加拿大 2 49
英國 1 40
英國 2 60app
Result:
國家 男 女
中國 340 260
美國 45 55
加拿大 51 49
英國 40 60dom
SELECT country, SUM( CASE WHEN sex = '1' THEN population ELSE 0 END), --male population SUM( CASE WHEN sex = '2' THEN population ELSE 0 END) --female population FROM Table_A GROUP BY country;
Cast is compatible to both sql server and mysql, convert is designed for sql server, and it can have more styles and specially useful for datetime (check datetime part)ide
select cast(1.23 as int) --return 1 select convert( int,1.23) --return 1
DECLARE @startnum INT=1000 --start DECLARE @endnum INT=1020 --end ; WITH gen AS ( SELECT @startnum AS num UNION ALL --change number+ i to adjust gap i SELECT num+3 FROM gen WHERE num+1<=@endnum ) SELECT * FROM gen option (maxrecursion 10000)
num
1000
1003
1006
1009
1012
1015
1018
1021oop
;WITH Split(stpos,endpos) AS( SELECT 0 AS stpos, CHARINDEX(',','Alice,Jack,Tom') AS endpos UNION ALL SELECT endpos+1, CHARINDEX(',','Alice,Jack,Tom',endpos+1) FROM Split WHERE endpos > 0 ) --LTRIM RTRIM to get rid of white space before start or after end of str SELECT RTRIM(LTRIM(SUBSTRING('Alice,Jack,Tom',stpos,COALESCE(NULLIF(endpos,0),LEN('Alice,Jack,Tom')+1)-stpos))) as name into #temp FROM Split
name
Alice
Jack
Tomsqlserver
SELECT * into #temp FROM (VALUES (1,'Alice'),(2,'Jack'),(3,'Tom')) AS t(id,name)
id name
1 Alice
2 Jack
3 Tomui
Select id, name, 'placeholder' as sex into #temp from A
Trick to copy a table structure(cols and datatype) but not content
--0=1 to not copy any rows Select id, name into #temp from A where 0=1 --the above query equals to select id, name into #temp from #temp1 delete from #temp1
create table #tmpStudent(Id int ,Name varchar(50),Age int) insert into #tmpStudent select id,name,age from #tmpStudentIDENTITY(1,1) PRIMARY KEY
DECLARE @temp Table ( Id int, Name varchar(20), Age int )
select GETDATE() select GETUTCDATE() select cast(GETDATE() as date) --date only SELECT convert(date, GETDATE() ) --date only
-- add or minus is on day basis select GETDATE()+1 --tomorrow select GETDATE()-1 --yesterday -- need to be 24.0 to return float select GETDATE()+1.0/24 --next hour select GETDATE()-1.0/24/2 --Last 30 min
--result is already datetime select DATEADD(yy,-2,'07/23/2009 13:23:44') --2 years ago select DATEADD(mm,5, DATEADD(dd,10,GETDATE())) --5 month and 10 days later
The datepart can be 'year' or 'yy' or 'yyyy', all same
select DATEDIFF(mi,GETDATE()+1.0/24, GETDATE()-1.0/24) -- return -120 select DATEDIFF(dd,'2019-11-23', '2019-12-23') --return 30
select cast('2019-10-23 23:30:59:883' as datetime) --'yyyy-mm-dd' select cast('2019/10/23 23:30:59:883' as datetime) --'yyyy/mm/dd' use ':' for ms select cast('10-23-2019 23:30:59.883' as datetime) --'mm-dd-yyyy' use '.' for ms select cast('10/23/2019 23:30:59.883' as datetime) --'mm/dd/yyyy' --same to use convert SELECT convert(date, '07/23/2009' )
--these pairs are same to get dd,mm,yy part of a datetime, return integer select Datepart(dd,GETDATE()),day(GETDATE()) select Datepart(mm,GETDATE()),month(GETDATE()) select Datepart(yyyy,GETDATE()),year(GETDATE()) select Datepart(dy,'2019-08-11') --get day of year: 223 select datename(mm,'2000-5-17') --return 'May' select datename(weekday,'2000-5-17') --return 'Wednesday'
-- not working!!!! return '2019-05-17', as it detect input is string, 103 is ignored select convert(varchar, '2019-05-17', 103) --input is datetime, reutrn formatted string '17/05/2019' select convert(varchar, cast('2019-05-17' as datetime), 103)
for a full list of datetime format code (smilar to 103)
DATE ONLY FORMATS | ||
Format # | Query | Sample |
---|---|---|
1 | select convert(varchar, getdate(), 1) | 12/30/06 |
2 | select convert(varchar, getdate(), 2) | 06.12.30 |
3 | select convert(varchar, getdate(), 3) | 30/12/06 |
4 | select convert(varchar, getdate(), 4) | 30.12.06 |
5 | select convert(varchar, getdate(), 5) | 30-12-06 |
6 | select convert(varchar, getdate(), 6) | 30 Dec 06 |
7 | select convert(varchar, getdate(), 7) | Dec 30, 06 |
10 | select convert(varchar, getdate(), 10) | 12-30-06 |
11 | select convert(varchar, getdate(), 11) | 06/12/30 |
12 | select convert(varchar, getdate(), 12) | 061230 |
23 | select convert(varchar, getdate(), 23) | 2006-12-30 |
101 | select convert(varchar, getdate(), 101) | 12/30/2006 |
102 | select convert(varchar, getdate(), 102) | 2006.12.30 |
103 | select convert(varchar, getdate(), 103) | 30/12/2006 |
104 | select convert(varchar, getdate(), 104) | 30.12.2006 |
105 | select convert(varchar, getdate(), 105) | 30-12-2006 |
106 | select convert(varchar, getdate(), 106) | 30 Dec 2006 |
107 | select convert(varchar, getdate(), 107) | Dec 30, 2006 |
110 | select convert(varchar, getdate(), 110) | 12-30-2006 |
111 | select convert(varchar, getdate(), 111) | 2006/12/30 |
112 | select convert(varchar, getdate(), 112) | 20061230 |
TIME ONLY FORMATS | ||
8 | select convert(varchar, getdate(), 8) | 00:38:54 |
14 | select convert(varchar, getdate(), 14) | 00:38:54:840 |
24 | select convert(varchar, getdate(), 24) | 00:38:54 |
108 | select convert(varchar, getdate(), 108) | 00:38:54 |
114 | select convert(varchar, getdate(), 114) | 00:38:54:840 |
DATE & TIME FORMATS | ||
0 | select convert(varchar, getdate(), 0) | Dec 12 2006 12:38AM |
9 | select convert(varchar, getdate(), 9) | Dec 30 2006 12:38:54:840AM |
13 | select convert(varchar, getdate(), 13) | 30 Dec 2006 00:38:54:840AM |
20 | select convert(varchar, getdate(), 20) | 2006-12-30 00:38:54 |
21 | select convert(varchar, getdate(), 21) | 2006-12-30 00:38:54.840 |
22 | select convert(varchar, getdate(), 22) | 12/30/06 12:38:54 AM |
25 | select convert(varchar, getdate(), 25) | 2006-12-30 00:38:54.840 |
100 | select convert(varchar, getdate(), 100) | Dec 30 2006 12:38AM |
109 | select convert(varchar, getdate(), 109) | Dec 30 2006 12:38:54:840AM |
113 | select convert(varchar, getdate(), 113) | 30 Dec 2006 00:38:54:840 |
120 | select convert(varchar, getdate(), 120) | 2006-12-30 00:38:54 |
121 | select convert(varchar, getdate(), 121) | 2006-12-30 00:38:54.840 |
126 | select convert(varchar, getdate(), 126) | 2006-12-30T00:38:54.840 |
127 | select convert(varchar, getdate(), 127) | 2006-12-30T00:38:54.840 |
create table #Student (id int, Class int, Score int ) insert into #Student values(1,1,88) insert into #Student values(2,1,66) insert into #Student values(3,2,30) insert into #Student values(4,2,70) insert into #Student values(5,2,60) insert into #Student values(6,3,70) insert into #Student values(7,3,80)
select * from students where id in ( select id FROM students group by id having count(*)>1 )
select * from students a right join ( select firstname, lastname from students group by firstname, lastname having count(*)>1 ) b on a.firstname=b.firstname and a.lastname=b.lastname
select * from students except( select a.* --need to select all columns here from students a right join ( select firstname, lastname from students group by firstname, lastname having count(*)>1 ) b on a.firstname=b.firstnameand a.lastname =b.lastname )
select distinct * from tableName --save the result equals to delete duplicated rows already
delete from #temp where id not in( select max(id) from #temp group by col1, col2 --the columns used when checking duplicate having count(*)>1 )
6.1 Delete directly from original table by "Partition" keyword
WITH tempVw AS ( SELECT *, ROW_NUMBER() OVER ( --over() is required for Row_Number() PARTITION BY --this reset the rowNumber to 1 for different group col1, col2 --which used as identifier to check duplicate ORDER BY --order by is required in Over() col1, col2 --keep same as above ) row_num FROM YourTable ) delete FROM tempVw WHERE row_num > 1 select * from YourTable --duplicated rows should be removed in original table
6.2 Add unique ID first so it is similar as point 5
--Use views to add rowId for table without unique id with tempVw as( select ROW_NUMBER() over (order by SurveyTypeid, surveyid ) as rowid,* from YourTable ) --define 2 views together, tempVw2 is all duplicated rows ,tempVw2 as ( select rowid,a.col1,a.col2 from tempVw a right join ( select col1, col2 from tempVw group by col1, col2 having count(*)>1 ) b on a.col1=b.col1 and a.col2=b.col2 ) --query after view, delete rows in view will delete original table delete from tempVw where rowid in ( --return all duplicated rows except 1 row for each group that we will keep select rowid from tempVw2 where rowid not in ( --return 1 row for each identifier of duplicated rows select min (rowid) from tempVw2 group by col1, col2 having count(*)>1 ) ) select * from YourTable --duplicated rows should be removed in original table
declare @sql varchar(max)= 'select * from #StudentMarks' exec (@sql)
--the end has 3 quote because: double single quote + end quote of the query varchar declare @sql varchar(max)= 'select * from #StudentMarks where name=''Alice''' exec (@sql)
-- output: 2019's, you need double the quote as it is in a string: 2019''s select *,'2019''s' as category from #StudentMarks where name='Alice' declare @sql varchar(max)= 'select *,''2019''''s'' as category from #StudentMarks where name=''Alice''' exec (@sql)
declare @Math int=90 declare @sql varchar(max)= 'select * from #StudentMarks where Math='+cast(@Math as varchar)+' and Science=40' exec (@sql)
declare @Name varchar(max)='Alice' --first 3 quotes = double the left quote of @Name + end quote of 1st part of query -- 4 quotes = start quote of 3rd part+ double the right quote of @Name + end quote of 3rd part declare @sql varchar(max)= 'select * from #StudentMarks where name='''+@Name+'''' exec (@sql)
--double the left and right quote around Alice inside the query declare @sql2 varchar(max)=' where name = ''Alice'' ' --do not forget to leave a space after each query declare @sql varchar(max)= 'select * from #StudentMarks '+ @sql2 exec (@sql)
Select * from A except Select * from B
Select * from A except Select * from B union all Select * from B except Select * from A
Select * from A Intersect Select * from B
declare @temp table(id int,Name varchar(50),sex varchar(10)) declare @sql varchar(max)= 'select id,name,''male'' from student where id<3' insert into @temp exec (@sql)
DECLARE @sql nvarchar(1000), @input varchar(75)='Fenton', @output varchar(75) SET @sql = 'SELECT top 1 @firstname=firstname FROM [AspNetUsers] WHERE surname = @surname' EXECUTE sp_executesql @sql, N'@surname varchar(75),@firstname varchar(75) OUTPUT', @surname = @input, @firstname=@output OUTPUT select @output
Select col1, col2 from A where exists (Select 1 from B where id=99) --inside exists you can select 1 or anything, it will return TRUE equally
Select id, name from A where not exists (Select 1 from B where B.id=A.id) --this equals to use IN keyword Select id, name from A where id not in (Select id from B)
--student number for each class select class,count (*) as total from Student group by class --average score for each class select class,avg(score) as AvgScore from Student group by class --highest score for each class select class,max(score) as HighestScore from Student group by class --total donation for each class select class,sum(donation) as TotalDonation from Student group by class
To get top x rows or the xth place in each group, use row_number()
SELECT * --INTO #Cars FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;Database=C:\cars.xlsx','SELECT * FROM [sheet1$]');
insert into #temp(id,name) values (1,'Alice'),(2, 'Jack')
insert into #temp(id,name, sex) select id, name,'male' from students where sex=1
declare @temp table(id int,Name varchar(50),sex varchar(10)) declare @sql varchar(max)= 'select id,name,''male'' from student where id<3' insert into @temp exec (@sql)
(https://blog.csdn.net/xiaolinyouni/article/details/6943337)
Select * from A cross join B Select * from A,B --same as above
Left join: contains all rows from left table A, if A.key=B.key, return result in new table, if multiple B.key match A.key, return multiple rows, if no B.key match, return row with null values
inner join: only return if A.key=B.key, can be one to one or one to many
(http://www.sqlservertutorial.net/sql-server-basics/sql-server-like/)
Select * from where name LIKE ' [^.$#@-]_ [a-z0-9]%'
C:\Users\*********\AppData\Roaming\Microsoft\SQL Server Management Studio\18.0\UserSettings.xml
<Element>.......</Element>
block that surrounds it.
Name Math Science English
Alice 90 40 60
Tom 30 20 10
CREATE TABLE #StudentMarks(Name varchar(100),Math int,Science int, English int) insert into #StudentMarks values('Alice',90,40,60) insert into #StudentMarks values('Tom',30,20,10)
name subject marks
Alice Math 90
Alice Science 40
Alice English 60
Tom Math 30
Tom Science 20
Tom English 10
--can be any col in #studentmarks + subject+ marks select name, subject, marks from #studentmarks unpivot ( marks --marks: actual values for subject in (Math, Science, English) --subject: col names ) a
select name, subject, case subject --this select the right subject score value to put in new col when 'Maths' then math when 'Science' then science when 'English' then english end as Marks from #studentmarks Cross Join (values('Maths'),('Science'),('English')) AS Subjct(Subject)
if it is static and only a few diff values, use "case when" (search 'case when' in blog)
CREATE TABLE #Source( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [FieldCode] [varchar](50) NULL, [Value] int NULL ) ON [PRIMARY] INSERT INTO #Source([Name],[FieldCode],[Value]) SELECT 'Alice','English',60 UNION ALL SELECT 'Jack','Math',70 UNION ALL SELECT 'Tom','Science',80 UNION ALL SELECT 'Tom','Math',75 UNION ALL SELECT 'Tom','English',57 UNION ALL SELECT 'Jack','English',80 UNION ALL SELECT 'Alice','Science',100
--**rename the reference col as name, sourceCol as 'fieldcode', value as 'value' DECLARE @SourceTable VARCHAR(500)='#Source' DECLARE @place_holder_len VARCHAR(500)='1000' --*****if get error 'String or binary data would be truncated', need increase size of this str --1000 length, this is random string placeholder of the col data length when using PIVOT function DECLARE @place_holder VARCHAR(8000)= space(@place_holder_len); --this should work as above --The max len of varchar is 8000, so need to divide sql str into a few varchar DECLARE @sql_current_str VARCHAR(8000) ='' DECLARE @sql_str1 VARCHAR(8000) ='' DECLARE @sql_str2 VARCHAR(8000) ='' DECLARE @sql_str3 VARCHAR(8000) ='' DECLARE @sql_str4 VARCHAR(8000) ='' DECLARE @sql_str5 VARCHAR(8000) ='' DECLARE @sql_str6 VARCHAR(8000) ='' DECLARE @sql_str7 VARCHAR(8000) ='' DECLARE @sql_str8 VARCHAR(8000) ='' DECLARE @sql_str9 VARCHAR(8000) ='' DECLARE @sql_str10 VARCHAR(8000) ='' DECLARE @sql_str998 VARCHAR(8000) ='' --to replace the placeholder to empty string and select result DECLARE @sql_str999 VARCHAR(8000) ='' --to replace the placeholder to empty string and select result DECLARE @all_survey_data_cols VARCHAR(8000) DECLARE @col VARCHAR(8000)='' --current col in loop of all the cols DECLARE @count int =1 DECLARE @index int =1 DECLARE @FieldTable Table (fieldcode varchar(1000)) SET @sql_str1 = 'select distinct (fieldcode) from '+ @SourceTable +' (nolock)' Insert @FieldTable Exec (@sql_str1) select *,ROW_NUMBER() over (order by fieldcode) as i into #Field_ASC from (select * from @FieldTable) a select @count=max(i) from #Field_ASC select @all_survey_data_cols = ISNULL(@all_survey_data_cols + ',','') + QUOTENAME(fieldcode) FROM (select fieldcode from #Field_ASC) a SET @sql_str1 = ' SELECT distinct name,data.* into #temp FROM '+ @SourceTable +' inner join ( SELECT * FROM (select fieldcode,'''+@place_holder+''' as zz from #Field_ASC) p PIVOT (max(zz) FOR fieldcode IN ( '+ @all_survey_data_cols +') ) AS pvt ) data on 1=1 ' while (@index<@count+1) begin select @col = fieldcode from #Field_ASC where i=@index --this is to divide the sql string as max size of varchar is 8000 if (len(@sql_str9)>7000) begin set @sql_current_str= @sql_str10 end else if (len(@sql_str8 )>7000) begin set @sql_current_str= @sql_str9 end else if (len(@sql_str7 )>7000) begin set @sql_current_str= @sql_str8 end else if (len(@sql_str6 )>7000) begin set @sql_current_str= @sql_str7 end else if (len(@sql_str5 )>7000) begin set @sql_current_str= @sql_str6 end else if (len(@sql_str4 )>7000) begin set @sql_current_str= @sql_str5 end else if (len(@sql_str3 )>7000) begin set @sql_current_str= @sql_str4 end else if (len(@sql_str2 )>7000) begin set @sql_current_str= @sql_str3 end else if (len(@sql_str1 )>7000) begin set @sql_current_str= @sql_str2 end else begin set @sql_current_str= @sql_str1 end set @sql_current_str =@sql_current_str +' update a set a.['+@col+']= CONVERT(varchar(8000), b.value) from #temp a inner join (select * from '+ @SourceTable +' (nolock) ) b on a.name=b.name and b.fieldcode='''+@col+ '''' --this is to divide the sql string as max size of varchar is 8000 if (len(@sql_str9)>7000) begin set @sql_str10 = @sql_current_str end else if (len(@sql_str8 )>7000) begin set @sql_str9 = @sql_current_str end else if (len(@sql_str7 )>7000) begin set @sql_str8 = @sql_current_str end else if (len(@sql_str6 )>7000) begin set @sql_str7 = @sql_current_str end else if (len(@sql_str5 )>7000) begin set @sql_str6 = @sql_current_str end else if (len(@sql_str4 )>7000) begin set @sql_str5 = @sql_current_str end else if (len(@sql_str3 )>7000) begin set @sql_str4 = @sql_current_str end else if (len(@sql_str2 )>7000) begin set @sql_str3 = @sql_current_str end else if (len(@sql_str1 )>7000) begin set @sql_str2 = @sql_current_str end else begin set @sql_str1 = @sql_current_str end set @index=@index+1 end set @index=1 while (@index<@count+1) begin select @col = fieldcode from #Field_ASC where i=@index if (len(@sql_str998)>7000) --if exceed 8000 size then store the rest in new str begin set @sql_str999 =@sql_str999 +' update #temp set ['+ @col +'] ='''' where len(['+ @col+']) =0' --replace placeholder by '' end else begin set @sql_str998 =@sql_str998 +' update #temp set ['+ @col +'] ='''' where len(['+ @col+']) =0'--replace placeholder by '' end set @index=@index+1 end set @sql_str999 =@sql_str999 + ' select * from #temp' PRINT ( @sql_str1 + @sql_str2+ @sql_str3 + @sql_str4 + @sql_str5 + @sql_str6 + @sql_str7+ @sql_str8 + @sql_str9 + @sql_str10 + @sql_str998+ @sql_str999 ) EXEC ( @sql_str1 + @sql_str2+ @sql_str3 + @sql_str4 + @sql_str5 + @sql_str6 + @sql_str7+ @sql_str8 + @sql_str9 + @sql_str10 + @sql_str998+ @sql_str999 ) --to check if the sql string is overflow print ('string 1: ' + cast (len ( @sql_str1 ) as varchar(100)) + ' string 2: ' + cast (len ( @sql_str2) as varchar(100)) +' string 3: ' + cast (len ( @sql_str3 ) as varchar(100))+' string 4: ' + cast (len ( @sql_str5 ) as varchar(100)) +' string 5: ' + cast (len ( @sql_str5 ) as varchar(100)) +' string 6: ' + cast (len ( @sql_str6 ) as varchar(100)) + ' string 7: ' + cast (len ( @sql_str7) as varchar(100)) +' string 8: ' + cast (len ( @sql_str8 ) as varchar(100)) +' string 9: ' + cast (len ( @sql_str9 ) as varchar(100))+' string 10: ' + cast (len ( @sql_str10 ) as varchar(100)) +' string 998: ' + cast (len ( @sql_str998 ) as varchar(100))+' string 999: ' + cast (len ( @sql_str999 ) as varchar(100))) drop table #Field_ASC
select NEWID() --315FC5A3-BE07-41BB-BE4F-75055729FA5B
SELECT CONVERT(varchar(255), NEWID())
SELECT RAND() -- 0<=decimal<1 SELECT RAND()*15+5; -- 5<=decimal<20 (if include 20 need *16) SELECT FLOOR(22.6) --22 SELECT CEILING(22.6) --23 SELECT ROUND(22.6,0) -- 23.0 SELECT ROUND(22.6,-1) --20.0
select *,row_number() over(order by class) rowid from #Student
select *,rank() over(order by class) rowid from #Student --if 1st has 2 pp, next is 3rd select *,dense_rank() over(order by class) rowid from #Student --if 1st has 2 pp, next is 2nd
select *,row_number() over(partition by class order by class) rowid from #Student
select * from ( select *,row_number() over(partition by class order by class) rowid from #Student )a where rowid<2 select * from ( select *,row_number() over(partition by class order by class) rowid from #Student )a where rowid=2
USE [YourDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* [SearchOneTable] 'ASPnetusers','2019' [SearchOneTable] 'ASPnetusers','8',1,1,0 */ CREATE PROC [dbo].[SearchOneTable] ( @TableName nvarchar(256), @Keyword nvarchar(100), @SearchDate bit=0, @SearchNumber bit=0, @SearchString bit=1 ) AS BEGIN SET NOCOUNT ON if PARSENAME(@TableName, 2) is null begin set @TableName = 'dbo.' + QUOTENAME(@TableName, '"') end SET @Keyword = QUOTENAME('%' + @Keyword + '%','''') DECLARE @ColumnName nvarchar(128) = '' DECLARE @ColumnNameTableVar table(ColumnName nvarchar(128)) --table var to store exec result into var DECLARE @sql varchar(max)='' DECLARE @StringTypes varchar(1000)='''char'', ''varchar'', ''nchar'', ''nvarchar''' DECLARE @DateTypes varchar(1000)='''date'', ''time'', ''datetime'', ''timestamp''' DECLARE @NumberTypes varchar(1000)='''int'', ''decimal'', ''float'', ''bit''' DECLARE @results TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630)) IF @TableName <> '' BEGIN WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) BEGIN set @sql='SELECT MIN(QUOTENAME(COLUMN_NAME)) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = PARSENAME('''+@TableName+''', 2) AND TABLE_NAME = PARSENAME('''+@TableName+''', 1) AND QUOTENAME(COLUMN_NAME) > '''+@ColumnName+''' AND DATA_TYPE IN (' + case when @SearchDate=1 and @SearchNumber=0 then @DateTypes +' , ' when @SearchDate=0 and @SearchNumber=1 then @NumberTypes +' , ' when @SearchDate=1 and @SearchNumber=1 then @DateTypes +' , ' + @NumberTypes +' , ' else ' ' end + case when @SearchString=1 then @StringTypes else '''''' end + ')' delete from @ColumnNameTableVar --empty table first insert into @ColumnNameTableVar exec ( @sql ) select top 1 @ColumnName=ColumnName from @ColumnNameTableVar IF @ColumnName IS NOT NULL BEGIN INSERT INTO @results EXEC ( 'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' WITH (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @Keyword ) END END END SELECT ColumnName, ColumnValue FROM @results END GO
USE [YourDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* *******don't use it in large db, which will be extremely slow********** [SearchAllTables] 'survey' */ CREATE PROC [dbo].[SearchAllTables] ( @Keyword nvarchar(100), @SearchDate bit=0, @SearchNumber bit=0, @SearchString bit=1 ) AS BEGIN SET NOCOUNT ON SET @Keyword = QUOTENAME('%' + @Keyword + '%','''') CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630)) DECLARE @TableName nvarchar(256)='' DECLARE @ColumnName nvarchar(128) DECLARE @ColumnNameTableVar table(ColumnName nvarchar(128)) --table var to store exec result into var DECLARE @sql varchar(max)='' DECLARE @StringTypes varchar(1000)='''char'', ''varchar'', ''nchar'', ''nvarchar'' ' DECLARE @DateTypes varchar(1000)='''date'', ''time'', ''datetime'', ''timestamp''' DECLARE @NumberTypes varchar(1000)='''int'', ''decimal'', ''float'', ''bit''' WHILE @TableName IS NOT NULL BEGIN SET @ColumnName = '' SET @TableName = ( SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName AND OBJECTPROPERTY( OBJECT_ID( QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) ), 'IsMSShipped' ) = 0 ) WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) BEGIN set @sql='SELECT MIN(QUOTENAME(COLUMN_NAME)) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = PARSENAME('''+@TableName+''', 2) AND TABLE_NAME = PARSENAME('''+@TableName+''', 1) AND QUOTENAME(COLUMN_NAME) > '''+@ColumnName+''' AND DATA_TYPE IN (' + case when @SearchDate=1 and @SearchNumber=0 then @DateTypes +' , ' when @SearchDate=0 and @SearchNumber=1 then @NumberTypes +' , ' when @SearchDate=1 and @SearchNumber=1 then @DateTypes +' , ' + @NumberTypes +' , ' else ' ' end + case when @SearchString=1 then @StringTypes else '''''' end + ')' delete from @ColumnNameTableVar --empty table first insert into @ColumnNameTableVar exec ( @sql ) select top 1 @ColumnName=ColumnName from @ColumnNameTableVar IF @ColumnName IS NOT NULL BEGIN INSERT INTO #Results EXEC ( 'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + 'WITH (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @Keyword ) END END END SELECT ColumnName, ColumnValue FROM #Results END GO
--to search "keyword" in any cols, in any tables of db SELECT b.name as tables, a.name as cols FROM sys.columns as a inner join sys.tables as b on a.object_id=b.object_id where a.name like '%keyword%' order by b.name
--to search "keyword" in stored procedure whole text SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LIKE '%keyword%' AND ROUTINE_TYPE='PROCEDURE'
Note: SQL index start from 1 not 0
select left('hello world',5) --return: hello select right('hello world!',6) --return:world!
select substring('hello world',7,5) --return: world
select REPLACE('123456','34','new') --return 12new56 select stuff('123456',3,2,'new') --same as above, start index=3, length=2
--split 'hello world' by space select left('hello world',CHARINDEX(' ','hello world')-1) select right('hello world',len('hello world')-CHARINDEX(' ','hello world'))
SELECT LTRIM(' Sample '); --return 'Sample ' SELECT RTRIM(' Sample '); --return ' Sample'
--char(13)+CHAR(10) = enter print 'first line'+char(13)+CHAR(10)+'Second line' --2 lines --char(9) is tab, the outsode replace delete all space print REPLACE(REPLACE(REPLACE(REPLACE('first line Second line',CHAR(13),''),CHAR(10),''),CHAR(9),''),' ','')
SELECT PATINDEX('%[mo]%', 'W3Schools.com'); --return m or o which appear first
select REPLICATE('hello world ',3) --return: hello world hello world hello world
select REVERSE('1234567') --return 7654321
select 'a'+SPACE(5)+'b' --return a b
select top 2 * from ( --between 4 and 5, 5-4+1=2 select top 5 * from #Student order by score desc)a order by score
-- if there are multiple highest score, will select highest score select * from ( select *,row_number() over( order by score desc) rowid from #Student )a where rowid=2 --if there are multiple highest score, still select second highest score select * from ( select *,rank() over( order by score desc) rowid from #Student )a where rowid=2
-- rowid between m and n -- rows between order of the m place to n place
Declare @isDebug bit=0 begin tran -- insert/update/delete queries if @isDebug=0 and @@error=0 --prod run and no errors begin commit tran end else -- test run or any error begin rollback tran end
Declare @isDebug bit=1 BEGIN TRY BEGIN tran if @isDebug=0 --test run begin -- insert/update/delete queries end else --prod run begin -- insert/update/delete queries end COMMIT tran --commit if above code has no error END TRY BEGIN CATCH ROLLBACK tran --if any error jump to this to rollback select ERROR_NUMBER() as ErrorNumber, ERROR_MESSAGE() as ErrorMessage, ERROR_PROCEDURE() as ErrorProcedure END CATCH
CREATE TABLE #temp (ID INT, Val VARCHAR(100)) INSERT #temp (ID, Val) VALUES (1,'FirstVal') INSERT #temp (ID, Val) VALUES (2,'SecondVal') CREATE TABLE #log (ID INT, Val VARCHAR(100), Query VARCHAR(100)) SELECT * FROM #temp INSERT #temp (ID, Val) output inserted.ID,inserted.Val,'Insert' into #log VALUES (3,'ThirdVal') --you can also output both deleted and inserted values Update #temp set Val='NewVal' output Deleted.ID, Deleted.Val,'Delete' into #log where id=3 Update #temp set Val='NewVal' output inserted.ID,inserted.Val,'Insert' into #log where id=3 DELETE FROM #temp OUTPUT Deleted.ID, Deleted.Val,'Delete' into #log WHERE ID IN (1,2) SELECT * FROM #log SELECT * FROM #temp DROP TABLE #temp DROP TABLE #log
1. Union not return duplicated rows (by duplicated mean all the values are exactly same)
2. Union All return all rows include duplicated rows
3. Both Union and Union all need to have exactly same number of total columns (col name can be diff but type need to be same)
4. Union All is much faster than Union
UPDATE a SET a.marks = b.marks FROM tempDataView a INNER JOIN tempData b ON a.Name = b.Name
declare @a int =1, @b varchar(max)='abc'
set @a=2
set @a=(select 2)
select @a=2, @b='new'
select top 1 @a=ClientId, @b=Surname from [AspNetUsers]
DECLARE @sql nvarchar(1000), @input varchar(75)='Fenton', @output varchar(75) SET @sql = 'SELECT top 1 @firstname=firstname FROM [AspNetUsers] WHERE surname = @surname' EXECUTE sp_executesql @sql, N'@surname varchar(75),@firstname varchar(75) OUTPUT', @surname = @input, @firstname=@output OUTPUT select @output
**delete or update view will influence original table, delete or update or insert values will influence on view
with StudentVw as( select top 100 ROW_NUMBER() over (order by SurveyTypeid, surveyid ) as rowid,* from ##temp order by channelid -- if use order by must have top keyword ) select * from StudentVw --must come with a query and only 1 query