SQL : BETWEEN vs =

They are identical: BETWEEN is a shorthand for the longer syntax in the question that includes both values (EventDate >= ’10/15/2009′ and EventDate <= ’10/19/2009′). Use an alternative longer syntax where BETWEEN doesn’t work because one or both of the values should not be included e.g. Select EventId,EventName from EventMaster where EventDate >= ’10/15/2009′ and … Read more

Is SQL IN bad for performance?

There are several considerations when writing a query using the IN operator that can have an affect on performance. First, IN clauses are generally internally rewritten by most databases to use the OR logical connective. So col IN (‘a’,’b’,’c’) is rewritten to: (COL = ‘a’) OR (COL = ‘b’) or (COL = ‘c’). The execution … Read more

Getting list of tables, and fields in each, in a database

Is this what you are looking for: Using OBJECT CATALOG VIEWS SELECT T.name AS Table_Name , C.name AS Column_Name , P.name AS Data_Type , P.max_length AS Size , CAST(P.precision AS VARCHAR) + “https://stackoverflow.com/” + CAST(P.scale AS VARCHAR) AS Precision_Scale FROM sys.objects AS T JOIN sys.columns AS C ON T.object_id = C.object_id JOIN sys.types AS P … Read more

SQL query to insert datetime in SQL Server

You will want to use the YYYYMMDD for unambiguous date determination in SQL Server. insert into table1(approvaldate)values(‘20120618 10:34:09 AM’); If you are married to the dd-mm-yy hh:mm:ss xm format, you will need to use CONVERT with the specific style. insert into table1 (approvaldate) values (convert(datetime,’18-06-12 10:34:09 PM’,5)); 5 here is the style for Italian dates. … Read more

Copy data into another table

If both tables are truly the same schema: INSERT INTO newTable SELECT * FROM oldTable Otherwise, you’ll have to specify the column names (the column list for newTable is optional if you are specifying a value for all columns and selecting columns in the same order as newTable‘s schema): INSERT INTO newTable (col1, col2, col3) … Read more