1.Difference between VARCHAR and NVARCHAR in SQL Server     S.No   Varchar[(n)]                         NVarchar[(n)]     ...
SELECT @firstName                     FirstName,DATALENGTH(@firstNa    FirstName,DATALENGTH(@firs            me) Length   ...
2.Difference between SQL Server and MySQL     S.No   SQL Server                      MySQL     1      Current Date and Tim...
tablename_id INTEGER     tablename_id INT IDENTITY         AUTO_INCREMENT PRIMARY     PRIMARY KEY                       KE...
3.Difference between SET QUOTED_IDENTIFIER ON and SET QUOTED_IDENTIFIEROFF in SQL Server     S.No   SET QUOTED_IDENTIFIER ...
QUTOED_IDENTIFIER                   QUTOED_IDENTIFIER ON/OFF:            ON/OFF:                                          ...
Result: 2011-09-16 13:23:18.767      6      +/- days:                          +/- days:             WORKS                ...
Upcoming SlideShare
Loading in …5
×

Sql server difference faqs- 9

2,199 views

Published on

This provides a SQL Server FAQS-9 which are of "Difference between" kind

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,199
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql server difference faqs- 9

  1. 1. 1.Difference between VARCHAR and NVARCHAR in SQL Server S.No Varchar[(n)] NVarchar[(n)] 1 Basic Definition: Basic Definition: Non-Unicode Variable Length UNicode Variable Length character character data type. data type. It can store both non- Unicode and Unicode (i.e. Japanese, Example: Korean etc) characters. DECLARE @FirstName AS VARCHAR(50) = ‘UMAR’ Example: SELECT @FirstName DECLARE @FirstName AS NVARCHAR(50)= ‘UMAR’ SELECT @FirstName 2 No. of Bytes required for each No. of Bytes required for each character: character: It takes 1 byte per character It takes 2 bytes per Unicode/Non- Unicode character. Example: DECLARE @FirstName AS Example: VARCHAR(50) = ‘UMAR’ DECLARE @FirstName AS SELECT @FirstName AS NVARCHAR(50)= ‘UMAR’ FirstName,DATALENGTH(@Firs SELECT @FirstName AS tName) AS Length FirstName,DATALENGTH(@FirstNa me) AS Length Result: FirstName Length Result: UMAR 4 FirstName Length UMAR 8 3 Optional Parameter n range: Optional Parameter n range: Optional Parameter n value can be Optional Parameter n value can be from 1 to 8000.Can store from 1 to 4000.Can store maximum maximum 8000 Non-Unicode 4000 Unicode/Non-Unicode characters characters. 4 If Optional Parameter n is not If Optional Parameter n is not specified in the variable specified in the variable declaration declaration or column or column definition: definition: If Optional parameter value n is not If Optional parameter value is not specified in the variable declaration or specified in the variable column definition then it is considered declaration or column definition as 2 then it is considered as 1. Example: Example: DECLARE @firstName NVARCHAR DECLARE @firstName =‘UMAR’ VARCHAR =‘UMAR’ SELECT @firstName
  2. 2. SELECT @firstName FirstName,DATALENGTH(@firstNa FirstName,DATALENGTH(@firs me) Length tName) Length Result: Result: FirstName Length FirstName Length U2 U15 If Optional Parameter n is not If Optional Parameter n is not specified in while using specified in while using CAST/CONVERT functions: CAST/CONVERT functions: When this optional parameter n is When this optional parameter n is not not specified while using the specified while using the CAST CAST/CONVERT functions, then CONVERT functions, then it is it is considered as 30. considered as 30. Example: Example: DECLARE @firstName DECLARE @firstName VARCHAR(35) =‘UMAR ASIA NVARCHAR(35) =‘UMAR ASIA INDIA TAMIL NADU INDIA TAMIL NADU CUDDALORE’ CUDDALORE’ SELECT CAST(@firstName AS SELECT CAST(@firstName AS VARCHAR) NVARCHAR) FirstName,DATALENGTH(CAS FirstName,DATALENGTH(CAST(@f T(@firstName AS VARCHAR)) irstName AS NVARCHAR)) Length Length Result: Result: FirstName Length FirstName Length UMAR ASIA INDIA TAMIL NADU UMAR ASIA INDIA TAMIL CUD 60 NADU CUD 307 Which one to use? Which one to use? If we know that data to be stored If we know that data to be stored in the in the column or variable doesn’t column or variable can have Unicode have any Unicode characters. characters.8 Storage Size: Storage Size: Takes no. of bytes equal to the no. Takes no. of bytes equal to twice the of Characters entered plus two no. of Characters entered plus two bytes extra for defining offset. bytes extra for defining offset.
  3. 3. 2.Difference between SQL Server and MySQL S.No SQL Server MySQL 1 Current Date and Time: Current Date and Time: SELECT GETDATE() SELECT NOW() Optionally: Use CURDATE() for the date only. 2 Limiting Results: Limiting Results: SELECT TOP 10 * FROM table SELECT * FROM table WHERE id = WHERE id = 1 1 LIMIT 10 3 Date Field Default Value: Date Field Default Value: DATETIME DEFAULT DATETIME fields cannot have a GETDATE() default value, i.e. "GETDATE()" We must use your INSERT statement to specify CURDATE() for the field. Optionally: Use datatype TIMESTAMP DEFAULT CURRENT_TIMESTAMP 4 Character Length: Character Length: LEN() CHARACTER_LENGTH() Aliases: CHAR_LENGTH(), LENGTH() 5 Character Replace: Character Replace: REPLACE() works case REPLACE() works case sensitively insensitively 6 Trim Functions: Trim Functions: LTRIM() and RTRIM() TRIM() 7 String Concatenation: String Concatenation: CONCATENATION USING + CONCAT(string, string), which (Does not automatically cast accepts two or more arguments. operands to compatible types) (Automatically casts values into types which can be concatenated) 8 Auto Increment Field Auto Increment Field Definition: Definition:
  4. 4. tablename_id INTEGER tablename_id INT IDENTITY AUTO_INCREMENT PRIMARY PRIMARY KEY KEY9 Get a List of Tables: Get a List of Tables: SP_TABLES SHOW TABLES10 Get Table Properties: Get Table Properties: HELP tablename DESCRIBE tablename11 Get Database Version: Get Database Version: SELECT @@VERSION SELECT VERSION()12 Recordset Paging: Recordset Paging: Recordset paging done by client Add to end of SQL: "LIMIT " & side-ADO (very involved) ((intCurrentPage-1)*intRecsPerPage) & ", " & intRecsPerPage LIMIT: The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).13 Get ID of Newest Inserted Get ID of Newest Inserted Record: Record: Two step process: SET NOCOUNT ON; INSERT 1. Execute your statement: INTO...; SELECT objConn.Execute("INSERT INTO...") id=@@IDENTITY; SET 2. Set objRS = NOCOUNT OFF; objConn.Execute("SELECT LAST_INSERT_ID() AS ID")14 Get a Random Record: Get a Random Record: SELECT TOP 1 * FROM Users SELECT * FROM Users ORDER BY ORDER BY NEWID() RAND() LIMIT 115 Generate a Unique GUID: Generate a Unique GUID: SELECT NEWID() SELECT UUID()
  5. 5. 3.Difference between SET QUOTED_IDENTIFIER ON and SET QUOTED_IDENTIFIEROFF in SQL Server S.No SET QUOTED_IDENTIFIER SET QUOTED_IDENTIFIER OFF ON 1 Characters Enclosed within Characters Enclosed within double double quotes: quotes: is treated as Identifier is treated as Literal 2 Try using Characters Enclosed Try using Characters Enclosed within double quotes as within double quotes as identifier: identifier: Fails Works Example: Below statement to create a Example: Below statement to table with table name “Table” Fails. create a table with table name SET QUOTED_IDENTIFIER OFF “Table” succeeds. GO SET QUOTED_IDENTIFIER ON CREATE TABLE dbo.”Table” GO (id int,”Function” VARCHAR(20)) CREATE TABLE dbo.”Table” GO (id int,”Function” Error Message: VARCHAR(20)) GO Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ‘Table’. 3 Try using Characters Enclosed Try using Characters Enclosed within double quotes as Literal: within double quotes as Literal: Fails Works Example: Below statement fails. Example: Below Statement Works. SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER OFF GO GO SELECT “BIRADAR” SELECT “UMAR” Error Message: Msg 207, Level 16, State 1, Line 1 Invalid column name ‘UMAR’. 4 Characters Enclosed within Characters Enclosed within single single quotes: quotes: is treated as Literal is treated as Literal Example: Example: SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER ON GO GO SELECT ‘UMAR’ SELECT ‘UMAR’ 5 How to find all the objects How to find all the objects which are which are created with SET created with SET
  6. 6. QUTOED_IDENTIFIER QUTOED_IDENTIFIER ON/OFF: ON/OFF: Below Statement can be used to find Below Statement can be used to all the objects created with SET find all the objects created with QUTOED_IDENTIFIER setting as SET QUTOED_IDENTIFIER OFF: setting as ON: SELECT OBJECT_NAME (object_id) SELECT OBJECT_NAME FROM sys.sql_modules WHERE (object_id) FROM uses_quoted_identifier = 0 sys.sql_modules WHERE uses_quoted_identifier = 14.Difference between DateTime and DateTime2 DataType S.No DateTime DateTime2[(n)] 1 Min Value: 1753-01-01 00:00:00 Min Value: 0001-01-01 00:00:00 2 Max Value: Max Value: 9999-12-31 23:59:59.997 9999-12-31 23:59:59.9999999 3 Storage Size: Storage Size: 8 Bytes 6 to 8 bytes Note: Parameter n is optional and if it is not specified then fractional seconds precision is 7 digit and it can be from 0 to 7 digit. For fractional seconds precision <3, takes 6 bytes For fractional seconds precision 3 or 4 it will take 7 bytes For fractional seconds precision >4 it will take 8 bytes 4 Usage: Usage: Declare @now datetime Declare @now datetime2(7) 5 Current Date and Time Current Date and Time function: function: SYSDATETIME()- It returns DB GetDate() – It returns DB Current Current Date and Time of DateTime2 Date and Time of DateTime Data Data Type Type Example: SELECT SYSDATETIME() Example: SELECT GETDATE() Result: 2011-09-16 13:23:18.7676720
  7. 7. Result: 2011-09-16 13:23:18.767 6 +/- days: +/- days: WORKS FAILS – Need to use only DateAdd Example: DECLARE function @nowDateTime DATETIME = Example: DECLARE GETDATE() @nowDateTime2 DATETIME2= SELECT @nowDateTime + 1 SYSDATETIME() Result: 2011-09-17 13:44:31.247 SELECT @nowDateTime2+1 Result: Msg 206, Level 16, State 2, Line 2 Operand type clash: datetime2 is incompatible with int 7 Compliance: Compliance: Is not an ANSI/ISO compliant Is an ANSI/ISO compliantPlease visit my blog @ http://onlydifferencefaqs.blogspot.in/

×