using …

Pure C#

‘Cast’ Kategorisi için Arşiv

SQL Server / T-SQL Tutorial

Yazan: esersahin 20/03/2009

http://www.java2s.com/Tutorial/SQLServer/CatalogSQLServer.htm

1. Query
1. 1. Query Select( 18 ) 1. 17. ANY( 6 )
1. 2. where( 10 ) 1. 18. Some( 2 )
1. 3. Order by( 16 ) 1. 19. AND( 2 )
1. 4. GROUP BY( 8 ) 1. 20. OR( 4 )
1. 5. Having( 7 ) 1. 21. Not( 6 )
1. 6. Null( 2 ) 1. 22. AS( 2 )
1. 7. IS NOT NULL( 1 ) 1. 23. Case( 19 )
1. 8. IS NULL( 3 ) 1. 24. arithmetic operator( 1 )
1. 9. Between( 8 ) 1. 25. Math Calculation( 4 )
1. 10. Exists( 6 ) 1. 26. Logical operator( 11 )
1. 11. In( 7 ) 1. 27. Comparison operators( 7 )
1. 12. DISTINCT( 7 ) 1. 28. Query Hints( 2 )
1. 13. LIKE( 16 ) 1. 29. Select Into( 6 )
1. 14. Column Alias( 5 ) 1. 30. TABLESAMPLE( 1 )
1. 15. Top( 10 ) 1. 31. ROWGUIDCOL( 1 )
1. 16. ALL( 8 ) 1. 32. rowversion( 1 )
2. Insert Delete Update
2. 1. Insert( 12 ) 2. 4. Delete( 9 )
2. 2. Insert into select( 5 ) 2. 5. Update( 16 )
2. 3. BULK INSERT( 1 ) 2. 6. Output( 1 )
3. Table
3. 1. Create Table( 6 ) 3. 5. DROP TABLE( 2 )
3. 2. ALTER TABLE( 16 ) 3. 6. Table alias( 5 )
3. 3. Column( 7 ) 3. 7. Temporary Table( 9 )
3. 4. Computed Columns( 1 ) 3. 8. TRUNCATE table( 2 )
4. Table Join
4. 1. Table Join( 14 ) 4. 7. Self join( 3 )
4. 2. Cross Join( 4 ) 4. 8. MERGE( 1 )
4. 3. Inner join( 8 ) 4. 9. One to many( 2 )
4. 4. Left Join( 1 ) 4. 10. many to many( 2 )
4. 5. LEFT OUTER JOIN( 5 ) 4. 11. APPLY( 1 )
4. 6. RIGHT OUTER JOIN( 2 )
5. Data Types
5. 1. bigint( 1 ) 5. 16. nchar( 1 )
5. 2. bit( 4 ) 5. 17. nVarChar( 2 )
5. 3. bitwise operators( 13 ) 5. 18. OPENROWSET( 2 )
5. 4. Char( 4 ) 5. 19. READTEXT( 1 )
5. 5. collate( 2 ) 5. 20. smalldatetime( 2 )
5. 6. Create Type( 5 ) 5. 21. Smallint( 1 )
5. 7. Data type( 1 ) 5. 22. SQL_VARIANT( 6 )
5. 8. Date Type( 1 ) 5. 23. text( 2 )
5. 9. datetime( 18 ) 5. 24. TEXTPTR( 1 )
5. 10. decimal( 4 ) 5. 25. timestamp( 2 )
5. 11. Float( 1 ) 5. 26. VARBINARY( 4 )
5. 12. FULLTEXT( 8 ) 5. 27. VARCHAR( 13 )
5. 13. integer( 9 ) 5. 28. WRITETEXT( 1 )
5. 14. Large Text( 3 ) 5. 29. Unicode( 1 )
5. 15. money( 1 )
6. Set Operations
6. 1. EXCEPT( 2 ) 6. 3. Union( 6 )
6. 2. INTERSECT( 2 )
7. Constraints
7. 1. Constraints( 5 ) 7. 7. Create Default( 2 )
7. 2. Primary Key( 7 ) 7. 8. Create Rule( 2 )
7. 3. Foreign Key( 9 ) 7. 9. NewID( 1 )
7. 4. REFERENCES( 5 ) 7. 10. Unique( 7 )
7. 5. Check( 13 ) 7. 11. uniqueidentifier( 1 )
7. 6. Default( 8 ) 7. 12. Constriant violation( 4 )
8. Subquery
8. 1. Subquery( 16 ) 8. 3. Inline view( 10 )
8. 2. Correlated subquery( 4 )
9. Aggregate Functions
9. 1. Aggregate function( 5 ) 9. 5. Min( 6 )
9. 2. AVG( 6 ) 9. 6. RANK( 3 )
9. 3. Count( 8 ) 9. 7. SUM( 6 )
9. 4. Max( 6 )
10. Date Functions
10. 1. CURRENT_TIMESTAMP( 4 ) 10. 8. DATEPART( 25 )
10. 2. Date calculation( 1 ) 10. 9. Day( 3 )
10. 3. DATEADD( 16 ) 10. 10. GETDATE( 7 )
10. 4. DATEDIFF( 16 ) 10. 11. GETUTCDATE( 1 )
10. 5. DATEFIRST( 3 ) 10. 12. ISDATE( 4 )
10. 6. DATEFORMAT( 1 ) 10. 13. MONTH( 2 )
10. 7. DATENAME( 15 ) 10. 14. Year( 2 )
11. Math Functions
11. 1. ABS( 2 ) 11. 13. LOG10( 1 )
11. 2. ACOS( 1 ) 11. 14. PI( 1 )
11. 3. ASIN( 1 ) 11. 15. Power( 2 )
11. 4. ATAN( 1 ) 11. 16. RADIANS( 1 )
11. 5. CEILING( 4 ) 11. 17. RAND( 4 )
11. 6. COS( 1 ) 11. 18. ROUND( 9 )
11. 7. COT( 1 ) 11. 19. SIGN( 1 )
11. 8. DEGREES( 1 ) 11. 20. Sin( 2 )
11. 9. EXP( 1 ) 11. 21. SQRT( 3 )
11. 10. FLOOR( 7 ) 11. 22. SQUARE( 2 )
11. 11. ISNUMERIC( 5 ) 11. 23. TAN( 1 )
11. 12. LOG( 1 )
12. String Functions
12. 1. ASCII( 3 ) 12. 13. REPLACE( 1 )
12. 2. Char function( 4 ) 12. 14. REPLICATE( 5 )
12. 3. CHARINDEX( 6 ) 12. 15. REVERSE( 2 )
12. 4. DIFFERENCE( 7 ) 12. 16. Right( 4 )
12. 5. FORMATMESSAGE( 1 ) 12. 17. RTRIM( 6 )
12. 6. LEFT( 7 ) 12. 18. SOUNDEX( 4 )
12. 7. LEN( 4 ) 12. 19. SPACE( 2 )
12. 8. LOWER( 4 ) 12. 20. STR( 6 )
12. 9. LTRIM( 4 ) 12. 21. STUFF( 3 )
12. 10. nchar function( 3 ) 12. 22. SUBSTRING( 8 )
12. 11. PATINDEX( 5 ) 12. 23. UNICODE function( 2 )
12. 12. QUOTENAME( 2 ) 12. 24. UPPER( 4 )
13. Data Convert Functions
13. 1. CAST( 7 ) 13. 4. DROP DATABASE( 2 )
13. 2. COALESCE( 5 ) 13. 5. ISNULL( 7 )
13. 3. CONVERT( 26 ) 13. 6. NULLIF( 1 )
14. Analytical Functions
14. 1. COMPUTE( 5 ) 14. 8. ROLLUP( 8 )
14. 2. CUBE( 7 ) 14. 9. ROW_NUMBER( 2 )
14. 3. DENSE_RANK( 1 ) 14. 10. STDEV( 3 )
14. 4. GROUPING( 4 ) 14. 11. STDEVP( 2 )
14. 5. NTILE( 1 ) 14. 12. VAR( 2 )
14. 6. PARTITION( 3 ) 14. 13. VARP( 1 )
14. 7. PIVOT( 1 )
15. Sequence Indentity
15. 1. Identity( 22 ) 15. 5. IDENT_INCR( 2 )
15. 2. IDENTITYCOL( 2 ) 15. 6. IDENT_SEED( 2 )
15. 3. IDENTITY_INSERT( 2 ) 15. 7. SCOPE_IDENTITY( 3 )
15. 4. IDENT_CURRENT( 2 )
16. View
16. 1. View( 8 ) 16. 5. DROP VIEW( 3 )
16. 2. Create View( 10 ) 16. 6. Filter view( 5 )
16. 3. Query view( 1 ) 16. 7. Updatable View( 6 )
16. 4. ALTER VIEW( 2 )
17. Index
17. 1. CREATE INDEX( 18 ) 17. 2. DROP INDEX( 2 )
18. Cursor
18. 1. Cursor( 3 ) 18. 5. FETCH_STATUS( 1 )
18. 2. Declare Cursor( 12 ) 18. 6. Close cursor( 1 )
18. 3. Open Cursor( 1 ) 18. 7. DEALLOCATE cursor( 2 )
18. 4. Fetch cursor( 3 ) 18. 8. cursor_status( 1 )
19. Database
19. 1. Create Database( 9 ) 19. 4. BACKUP( 3 )
19. 2. Use( 1 ) 19. 5. RESTORE DATABASE( 2 )
19. 3. Alter Database( 8 )
20. Transact SQL
20. 1. Declare( 7 ) 20. 14. Delete Statement( 5 )
20. 2. Variable( 15 ) 20. 15. Insert Statement( 5 )
20. 3. Variable Select( 8 ) 20. 16. Select statement( 4 )
20. 4. Set( 7 ) 20. 17. Update Statement( 2 )
20. 5. Table Variable( 5 ) 20. 18. Dynamic SQL( 8 )
20. 6. Print( 5 ) 20. 19. EXEC( 12 )
20. 7. IF( 14 ) 20. 20. Error Handler( 1 )
20. 8. while( 10 ) 20. 21. RAISERROR( 9 )
20. 9. Goto( 3 ) 20. 22. Return( 7 )
20. 10. Break( 1 ) 20. 23. Try catch( 7 )
20. 11. Comments( 2 ) 20. 24. WAITFOR( 4 )
20. 12. Continue( 1 ) 20. 25. With( 4 )
20. 13. Code Block( 2 )
21. Procedure Function
21. 1. Create function( 6 ) 21. 9. Procedure call( 2 )
21. 2. function( 19 ) 21. 10. Alter procedure( 4 )
21. 3. Recursive function( 5 ) 21. 11. Utility Procedure( 5 )
21. 4. Alter function( 1 ) 21. 12. Drop procedure( 2 )
21. 5. Drop function( 2 ) 21. 13. Parameter( 20 )
21. 6. Utility function( 6 ) 21. 14. Output parameter( 3 )
21. 7. Create Procedure( 6 ) 21. 15. Returning value( 4 )
21. 8. Procedure( 10 )
22. Trigger
22. 1. Trigger( 14 ) 22. 10. Trigger order( 2 )
22. 2. Alter Trigger( 4 ) 22. 11. Drop trigger( 2 )
22. 3. Trigger for after( 4 ) 22. 12. COLUMNS_UPDATED( 1 )
22. 4. Trigger for Delete( 4 ) 22. 13. Update function( 3 )
22. 5. Trigger for insert( 1 ) 22. 14. Deleted table( 2 )
22. 6. Trigger for update( 4 ) 22. 15. Inserted table( 5 )
22. 7. Trigger on database( 2 ) 22. 16. RECURSIVE_TRIGGERS( 1 )
22. 8. Trigger on server( 1 ) 22. 17. Utility trigger( 4 )
22. 9. Trigger on view( 3 )
23. Transaction
23. 1. TRANSACTION( 14 ) 23. 4. Save points( 2 )
23. 2. TRANSACTION ISOLATION LEVEL( 3 ) 23. 5. TRANCOUNT( 2 )
23. 3. COMMIT( 1 )
24. XML
24. 1. XML( 7 ) 24. 4. XML query( 5 )
24. 2. For XML( 4 ) 24. 5. XML Schema( 1 )
24. 3. XML Index( 1 )
25. System Functions
25. 1. APP_NAME( 1 ) 25. 31. sp_depends( 1 )
25. 2. COL_LENGTH( 2 ) 25. 32. sp_describe_cursor( 1 )
25. 3. COL_NAME( 1 ) 25. 33. sp_dropdevice( 1 )
25. 4. context_info( 1 ) 25. 34. sp_DropLogin( 1 )
25. 5. DATABASEPROPERTY( 1 ) 25. 35. sp_dropmessage( 1 )
25. 6. DATABASEPROPERTYEX( 1 ) 25. 36. sp_DropRoleMember( 3 )
25. 7. DATALENGTH( 4 ) 25. 37. sp_DropSrvRoleMember( 2 )
25. 8. db_name( 2 ) 25. 38. sp_droptype( 1 )
25. 9. DecryptByPassphrase( 1 ) 25. 39. sp_GrantDbAccess( 2 )
25. 10. EncryptByPassPhrase( 1 ) 25. 40. sp_GrantLogin( 1 )
25. 11. fn_builtin_permissions( 1 ) 25. 41. sp_help( 3 )
25. 12. fn_helpcollations( 1 ) 25. 42. sp_helpconstraint( 2 )
25. 13. fn_varbintohexstr( 1 ) 25. 43. sp_helpdb( 2 )
25. 14. fn_varbintohexsubstring( 1 ) 25. 44. sp_helpindex( 1 )
25. 15. fn_virtualfilestats( 1 ) 25. 45. sp_HelpRole( 2 )
25. 16. sp_addlinkedserver( 1 ) 25. 46. sp_helptext( 4 )
25. 17. sp_addlogin( 2 ) 25. 47. sp_helpuser( 1 )
25. 18. sp_addmessage( 3 ) 25. 48. sp_makestartup( 1 )
25. 19. sp_AddRole( 2 ) 25. 49. sp_monitor( 1 )
25. 20. sp_addrolemember( 5 ) 25. 50. sp_procoption( 1 )
25. 21. sp_AddSrvRoleMember( 2 ) 25. 51. sp_recompile( 2 )
25. 22. sp_addtype( 3 ) 25. 52. sp_refreshview( 1 )
25. 23. sp_altermessage( 1 ) 25. 53. sp_rename( 2 )
25. 24. sp_bindefault( 2 ) 25. 54. sp_RevokeDbAccess( 2 )
25. 25. sp_bindrule( 1 ) 25. 55. sp_serveroption( 1 )
25. 26. sp_change_users_login( 1 ) 25. 56. sp_spaceused( 2 )
25. 27. sp_configure( 4 ) 25. 57. sp_unbindefault( 1 )
25. 28. sp_create_removable( 1 ) 25. 58. sp_unmakestartup( 1 )
25. 29. sp_dboption( 9 ) 25. 59. SYSTEM_USER( 3 )
25. 30. sp_DenyLogin( 1 )
26. System Settings
26. 1. ANSI_NULLS( 1 ) 26. 24. PACK_RECEIVED( 1 )
26. 2. CONNECTIONS( 1 ) 26. 25. PACK_SENT( 1 )
26. 3. CPU_BUSY( 1 ) 26. 26. QUOTED_IDENTIFIER( 1 )
26. 4. DBCC( 10 ) 26. 27. rowcount( 7 )
26. 5. DB_ID( 1 ) 26. 28. ROWCOUNT_BIG( 1 )
26. 6. ERROR( 11 ) 26. 29. SERVERNAME( 1 )
26. 7. ERROR_NUMBER( 1 ) 26. 30. SERVERPROPERTY( 2 )
26. 8. ERROR_SEVERITY( 1 ) 26. 31. SERVICENAME( 1 )
26. 9. FORCEPLAN( 1 ) 26. 32. SESSIONPROPERTY( 7 )
26. 10. HashBytes( 1 ) 26. 33. SESSION_USER( 1 )
26. 11. HOST_ID( 1 ) 26. 34. SHOWPLAN_TEXT( 2 )
26. 12. HOST_NAME( 1 ) 26. 35. SPID( 1 )
26. 13. IDLE( 1 ) 26. 36. SUSER_ID( 1 )
26. 14. Information_schema( 10 ) 26. 37. SUSER_NAME( 1 )
26. 15. IO_BUSY( 1 ) 26. 38. TIMETICKS( 1 )
26. 16. LANGID( 1 ) 26. 39. TOTAL_ERRORS( 1 )
26. 17. LOCK_TIMEOUT( 2 ) 26. 40. TOTAL_READ( 1 )
26. 18. NESTLEVEL( 1 ) 26. 41. TOTAL_WRITE( 1 )
26. 19. NOCOUNT( 1 ) 26. 42. VERSION( 2 )
26. 20. OBJECTPROPERTY( 4 ) 26. 43. XACT_ABORT( 3 )
26. 21. OBJECT_ID( 5 ) 26. 44. xp_cmdshell( 2 )
26. 22. OBJECT_NAME( 1 ) 26. 45. xp_msver( 2 )
26. 23. PACKET_ERRORS( 1 ) 26. 46. xp_sendmail( 1 )
27. System Tables Views
27. 1. sys.columns( 1 ) 27. 19. sys.triggers( 3 )
27. 2. sys.databases( 3 ) 27. 20. sys.types( 1 )
27. 3. sys.database_principals( 2 ) 27. 21. sys.views( 3 )
27. 4. sys.fn_varbintohexstr( 1 ) 27. 22. sys.xml_schema_collections( 1 )
27. 5. sys.fulltext_catalogs( 1 ) 27. 23. sys.xml_schema_namespaces( 1 )
27. 6. sys.fulltext_indexes( 1 ) 27. 24. syscolumns( 2 )
27. 7. sys.fulltext_index_columns( 1 ) 27. 25. syscomments( 3 )
27. 8. sys.indexes( 1 ) 27. 26. sysconstraints( 2 )
27. 9. sys.messages( 1 ) 27. 27. sysdatabases( 2 )
27. 10. sys.objects( 4 ) 27. 28. sysdepends( 2 )
27. 11. sys.schemas( 3 ) 27. 29. sysfiles( 1 )
27. 12. sys.server_principals( 1 ) 27. 30. sysindexes( 3 )
27. 13. sys.server_sql_modules( 1 ) 27. 31. syslogins( 1 )
27. 14. sys.server_triggers( 1 ) 27. 32. sysmessages( 1 )
27. 15. sys.sql_dependencies( 1 ) 27. 33. sysobjects( 5 )
27. 16. sys.sql_modules( 2 ) 27. 34. systypes ( 1 )
27. 17. sys.syscacheobjects( 1 ) 27. 35. SYSUSERS( 2 )
27. 18. sys.syslogins( 1 )
28. User Role
28. 1. CREATE CERTIFICATE( 2 ) 28. 8. REVOKE( 6 )
28. 2. CREATE SCHEMA( 1 ) 28. 9. Role( 11 )
28. 3. CURRENT_USER( 1 ) 28. 10. schema( 3 )
28. 4. ALTER AUTHORIZATION( 1 ) 28. 11. USER( 8 )
28. 5. DENY( 2 ) 28. 12. USER_ID( 1 )
28. 6. GRANT( 14 ) 28. 13. USER_NAME( 1 )
28. 7. LOGIN( 13 )
29. CLR
29. 1. ASSEMBLY( 4 ) 29. 2. CLR( 6 )

Yazı kategorisi: CTE, Cast, Common Table Expressions, Cross-Tabs, Functions, Instead Of, Pivot Tables, ROW_NUMBER, Raiserror, SQL, SQLCLR, Sql Server, Sql Server Error, Sql Server Join, Stored Procedure, Transact-SQL, Trigger, UNION, UNION ALL, Update with Join | » yorum bırak;

SQL Server / T-SQL

Yazan: esersahin 20/03/2009

http://www.java2s.com/Code/SQLServer/CatalogSQLServer.htm

Select Query Subquery Insert Delete Update Data Set
Select Query
ALL( 1 ) AND( 1 ) ANY( 1 ) AS( 3 ) Between AND( 5 ) Bitwise Operator( 1 )
Case( 9 ) Column Alias( 4 ) Compare Date( 2 ) Data Type( 1 ) Date Calculation( 1 ) Date Compare( 1 )
DESC( 1 ) DISTINCT( 3 ) EXISTS( 5 ) Group BY( 9 ) HAVING( 6 ) IN( 6 )
IS NULL( 2 ) ISNULL( 6 ) LIKE( 10 ) Logical Operator( 7 ) Math Operator( 2 ) MAXRECURSION( 1 )
NOT( 4 ) NULL( 3 ) Operator Precedence( 2 ) OR( 2 ) Order Columns( 5 ) Order( 11 )
Query Batch( 1 ) Query Columns( 6 ) Quotation Mark( 1 ) Select Into( 3 ) Select( 13 ) String Compare( 1 )
Top percent( 4 ) Top With Ties( 1 ) Top( 7 ) Where( 9 ) With( 2 )
Subquery
Correlated subquery( 6 ) Nested Subquery( 2 ) RTRIM( 4 ) Subqueries ALL( 1 ) Subqueries ANY( 2 ) Subqueries Exists( 4 )
Subqueries IN( 5 ) Subqueries( 4 ) Subquery Aggregate Functions( 2 ) Subquery From( 1 )
Insert Delete Update
Batch Insert( 2 ) BULK INSERT( 1 ) Delete( 6 ) Insert Image( 1 ) Insert Select( 4 ) Insert( 5 )
Update( 15 )
Data Set
EXCEPT( 1 ) INTERSECT( 1 ) UNION ALL( 1 ) Union( 3 )
Store Procedure Function Transact SQL Trigger
Store Procedure Function
Create Function( 5 ) Create Procedure( 13 ) Default Parameter( 3 ) Drop Procedure( 1 ) EXEC( 6 ) EXECUTE( 2 )
Function Call( 2 ) Function Parameters( 1 ) Function Return( 7 ) Procedure Call( 3 ) Procedure Parameters( 2 ) Recursive function( 1 )
Scalar Functions( 1 )
Transact SQL
Assign Value( 4 ) ATAN( 1 ) Break( 2 ) COALESCE( 1 ) Code Block( 2 ) Continue( 2 )
Declare Variables( 5 ) Delete Data( 1 ) Dynamic SQL( 4 ) Error( 4 ) GOTO( 1 ) Handle Error( 1 )
If( 7 ) Insert Data( 6 ) LOG( 2 ) Object Property( 1 ) Print( 1 ) Query Data( 2 )
RAISERROR( 2 ) Return( 2 ) RowCount( 3 ) schemas( 1 ) Select Data( 1 ) Select Variables( 2 )
Set Variables( 4 ) Statement( 1 ) Table Variable( 5 ) Update Data( 5 ) Variable Scope( 2 ) Variables( 1 )
Version( 1 ) WAITFOR( 2 ) While( 5 )
Trigger
Alter Trigger( 1 ) Create Trigger( 3 ) Disable Trigger( 1 ) Enable Disable Trigger( 1 ) inserted table( 1 ) INSTEAD OF Trigger( 3 )
Trigger after( 2 ) Trigger for Insert( 1 ) Trigger for update( 4 ) Trigger Value( 2 ) UPDATE function( 1 )
Table Table Joins View Index Database
Table
Add Column( 3 ) Alter Table( 2 ) Column Data Type( 1 ) Column Index( 1 ) Computed Column( 2 ) Copy Data( 1 )
Create Table( 1 ) Derived Columns( 1 ) Drop Column( 2 ) Drop Table( 3 ) Rename Column( 1 ) Rename Table( 1 )
Sub query( 2 ) Table Alias( 1 ) Table Copy( 1 ) Table Existence( 1 ) Table Properties( 1 ) Temporary Table( 3 )
TRUNCATE TABLE( 2 )
Table Joins
ANSI Join Syntax( 3 ) Cross Join( 3 ) INNER JOIN( 4 ) Left Join( 2 ) LEFT OUTER JOIN( 1 ) RIGHT JOIN( 1 )
RIGHT OUTER JOIN( 2 ) Self Join( 1 ) SQL Server Join Syntax( 2 ) Table Join( 10 )
View
Alter View( 2 ) Create View( 13 ) Drop View( 2 ) ENCRYPTION( 1 ) Filter view( 2 ) Inline view( 2 )
Nested View( 1 ) Query View( 1 ) SCHEMABINDING( 1 ) Updatable View( 2 ) View Existance( 1 )
Index
CLUSTERED INDEX( 2 ) Create Index( 6 ) Drop Index( 1 ) DROP_EXISTING( 1 )
Database
Create Database( 3 ) Database Info( 1 ) Database Option( 7 ) databases( 2 ) DROP DATABASE( 2 ) USE( 1 )
Transaction Cursor Constraints Sequence
Transaction
Commit Transaction( 3 ) Transaction Roll back( 4 ) XACT_ABORT( 1 )
Cursor
Declare CURSOR( 3 ) Fetch Data( 2 ) Fetch_Status( 1 ) Open cursor( 2 )
Constraints
Add Primary Key( 2 ) CASCADE( 1 ) Check Options( 6 ) Check value range( 3 ) Column Constraints( 5 ) Constraints Basics( 2 )
CREATE RULE( 1 ) Default Value( 3 ) Disable constraint( 1 ) Foreign Key( 4 ) Maintain Constaints( 1 ) Nullable( 3 )
Primary key( 3 ) Unique( 3 ) UniqueIdentifier( 1 )
Sequence
IDENTITY( 12 ) IDENTITYCOL( 1 ) IDENT_CURRENT( 2 ) NEWID( 1 ) Procedure Call( 3 ) Procedure Existance( 1 )
Procedure Permission( 1 ) Procedure with Parameters( 13 ) Procedure( 1 )
Data Type XML Date Timezone
Data Type
Bit( 2 ) Cast( 21 ) char( 1 ) Convert( 8 ) Data Type Convert( 1 ) datetime( 1 )
decimal( 7 ) float( 1 ) int( 3 ) Money( 1 ) nchar Data Type( 1 ) Number Format( 2 )
text( 1 ) varchar( 3 )
XML
XML column( 2 )
Date Timezone
CURRENT_TIMESTAMP( 1 ) Date Data Type( 2 ) Date Format( 12 ) DATEADD( 18 ) DATEDIFF( 19 ) DATENAME( 14 )
DATEPART( 13 ) Datetime( 3 ) Day( 4 ) GetDate( 4 ) GETUTCDATE( 2 ) Int( 1 )
ISDATE( 6 ) Month( 4 ) Year( 4 )
Aggregate Functions Analytical Functions Math Functions String Functions
Aggregate Functions
Aggregate Function Basics( 2 ) AVG( 4 ) Count( 6 ) COUNT_BIG( 2 ) MAX( 3 ) MIN( 2 )
SUM( 2 )
Analytical Functions
COMPUTE( 5 ) Cube( 2 ) DENSE RANK( 2 ) NTile( 1 ) Over( 1 ) RANK( 2 )
Row Number( 3 ) STDEV( 4 ) VAR( 3 ) VARP( 2 ) WITH ROLLUP( 2 )
Math Functions
ABS( 2 ) ACOS( 2 ) ASIN( 1 ) CEILING( 3 ) COS( 2 ) COT( 1 )
DEGREES( 2 ) EXP( 1 ) FLOOR( 3 ) ISNUMERIC( 3 ) Log10( 2 ) mod( 1 )
PI( 1 ) Power( 2 ) RADIANS( 1 ) RAND( 1 ) ROUND( 9 ) SIGN( 1 )
SIN( 1 ) SQRT( 1 ) SQUARE( 1 ) TAN( 1 )
String Functions
ASCII( 2 ) CHAR( 3 ) CHARINDEX( 5 ) COLLATE( 1 ) DATALENGTH( 3 ) Difference( 4 )
Escape( 1 ) LEFT( 4 ) LEN( 4 ) Lower( 2 ) LTRIM( 2 ) NCHAR( 2 )
PATINDEX( 2 ) QUOTENAME( 1 ) Regular Expressions( 5 ) REPLACE( 3 ) REPLICATE( 4 ) REVERSE( 2 )
RIGHT( 4 ) SOUNDEX( 2 ) Space( 2 ) STR( 6 ) STUFF( 2 ) Substring( 4 )
UNICODE( 1 ) UPPER( 3 )
System
System
columns( 2 ) DATABASEPROPERTYEX( 1 ) DB_ID( 1 ) Error_Message( 1 ) HOST_NAME( 1 ) INFORMATION_SCHEMA( 3 )
LOCK_TIMEOUT( 1 ) LOGIN( 1 ) OBJECT_ID( 1 ) QUOTED_IDENTIFIER( 2 ) Row Count( 2 ) ROWCOUNT( 1 )
SHOWPLAN_TEXT( 2 ) sp_addmessage( 2 ) sp_altermessage( 1 ) sp_dboption( 1 ) sp_dropmessage( 1 ) sp_helpconstraint( 1 )
sp_helpindex( 1 ) sp_helptext( 1 ) sp_unbindefault( 1 ) STATISTICS( 1 ) SUSER_SNAME( 1 ) sys.sql_modules( 1 )
sys.syscacheobjects( 1 ) sys.types( 1 ) sys.views( 1 ) sys.XML_schema_collections( 1 ) syscomments( 1 ) sysdepends( 1 )
sysmessages( 2 ) sysobjects( 2 ) sysusers( 1 ) TOTAL_ERRORS( 1 ) TOTAL_READ( 1 ) USER( 1 )
USER_NAME( 1 ) views( 1 )

Yazı kategorisi: CTE, Cast, Cross-Tabs, Functions, Pivot Tables, SQL, Sql Server, Sql Server Error, Sql Server Join, Stored Procedure, Transact-SQL, Trigger | » yorum bırak;

CAST and CONVERT (Transact-SQL)

Yazan: esersahin 13/03/2009

http://msdn.microsoft.com/en-us/library/ms187928(SQL.90).aspx

Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.

Topic link icon Transact-SQL Syntax Conventions

Syntax for CAST:
CAST ( expression AS data_type [ (length ) ])

Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
expression
Is any valid expression.

data_type
Is the target system-supplied data type. This includes xml, bigint, and sql_variant. Alias data types cannot be used. For more information about available data types, see Data Types (Transact-SQL).

length
Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types. For CONVERT, if length is not specified, the default to 30 characters.

style
Is the style of the date format used to convert datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types), or to convert character data of known date or time formats to datetime or smalldatetime data; or the string format used to convert float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types). When style is NULL, the result returned is also NULL.

ms187928.note(en-US,SQL.90).gifNote:
The styles listed in this topic are supported by SQL Server in the described combinations with CONVERT target data types. All other styles and combinations are not supported. Do not use any unsupported styles. Using an unsupported style or an unsupported combination of style and target data type might return an error or unreliable results. These results are not guaranteed across releases of SQL Server.

SQL Server supports the date format in Arabic style by using the Kuwaiti algorithm.

In the following table, the two columns on the left represent the style values for converting datetime or smalldatetime data to character data. Add 100 to a style value to obtain a four-place year that includes the century (yyyy).

Without century (yy) (1) With century (yyyy) Standard Input/Output (3)
- 0 or 100 (1, 2) Default mon dd yyyy hh:miAM (or PM)
1 101 U.S. mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 British/French dd/mm/yy
4 104 German dd.mm.yy
5 105 Italian dd-mm-yy
6 106 (1) - dd mon yy
7 107 (1) - Mon dd, yy
8 108 - hh:mi:ss
- 9 or 109 (1, 2) Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 110 USA mm-dd-yy
11 111 JAPAN yy/mm/dd
12 112 ISO yymmdd
- 13 or 113 (1, 2) Europe default + milliseconds dd mon yyyy hh:mi:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 or 120 (2) ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
- 21 or 121 (2) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)
- 126 (4) ISO8601 yyyy-mm-ddThh:mi:ss.mmm (no spaces)
127(6, 7) ISO8601 with time zone Z. yyyy-mm-ddThh:mi:ss.mmmZ

(no spaces)

- 130 (1, 2) Hijri (5) dd mon yyyy hh:mi:ss:mmmAM
- 131 (2) Hijri (5) dd/mm/yy hh:mi:ss:mmmAM

1 These style values return nondeterministic results. Includes all (yy) (without century) styles and a subset of (yyyy) (with century) styles.

2 The default values (style 0 or 100, 9 or 109, 13 or 113, 20 or 120, and 21 or 121) always return the century (yyyy).

3 Input when you convert to datetime; output when you convert to character data.

4 Designed for XML use. For conversion from datetime or smalldatetime to character data, the output format is as described in the previous table.

5 Hijri is a calendar system with several variations. SQL Server 2005 uses the Kuwaiti algorithm.

ms187928.note(en-US,SQL.90).gifImportant:
By default, SQL Server interprets two-digit years based on a cutoff year of 2049. That is, the two-digit year 49 is interpreted as 2049 and the two-digit year 50 is interpreted as 1950. Many client applications, such as those based on Automation objects, use a cutoff year of 2030. SQL Server provides the two digit year cutoff configuration option that changes the cutoff year used by SQL Server and allows for the consistent treatment of dates. We recommend specifying four-digit years.

6 Only supported when casting from character data to datetime or smalldatetime. When character data that represents only date or only time components is cast to the datetime or smalldatetime data types, the unspecified time component is set to 00:00:00.000, and the unspecified date component is set to 1900-01-01.

7The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or – direction. For example: 2006-12-12T23:45:12-08:00.

When you convert to character data from smalldatetime, the styles that include seconds or milliseconds show zeros in these positions. You can truncate unwanted date parts when you convert from datetime or smalldatetime values by using an appropriate char or varchar data type length.

The following table shows the values for style that can be used for converting float or real to character data.

Value Output
0 (default) A maximum of 6 digits. Use in scientific notation, when appropriate.
1 Always 8 digits. Always use in scientific notation.
2 Always 16 digits. Always use in scientific notation.
ms187928.note(en-US,SQL.90).gifNote:
If style 126 is specified for conversion from float and real the output is equivalent to the style value of 2 and 1, respectively.

The following table shows the values for style that can be used for converting money or smallmoney to character data.

Value Output
0 (default) No commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 4235.98.
1 Commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 3,510.92.
2 No commas every three digits to the left of the decimal point, and four digits to the right of the decimal point; for example, 4235.9819.
ms187928.note(en-US,SQL.90).gifNote:
If style 126 is specified for conversion from money, or smallmoney to character data, the output is equivalent to the style value of 2.

The following table shows the values for style that can be used for converting string input to xml data.

Value Output
0 (default) Use default parsing behavior that discards insignificant white space and does not allow for an internal DTD subset.

When you convert to the xml data type, SQL Server 2005 insignificant white space is handled differently than in XML 1.0. For more information, see Generating XML Instances.
ms187928.note(en-US,SQL.90).gifNote:
1 Preserve insignificant white space. This style setting sets the default xml:space handling to behave the same as if xml:space=”preserve” has been specified instead.
2 Enable limited internal DTD subset processing.

If enabled, the server can use the following information that is provided in an internal DTD subset to perform nonvalidating parse operations.

  • Defaults for attributes are applied.
  • Internal entity references are resolved and expanded.
  • The DTD content model will be checked for syntactical correctness.

The parser will ignore external DTD subsets. It also does not evaluate the XML declaration to see whether the standalone attribute is set yes or no, but instead parses the XML instance as if it is a stand-alone document.

3 Preserve insignificant white space and enable limited internal DTD subset processing.
Returns the same value as data_type.
Implicit conversions are those conversions that occur without specifying either the CAST or CONVERT function. Explicit conversions are those conversions that require the CAST or CONVERT function to be specified. The following illustration shows all explicit and implicit data type conversions that are allowed for SQL Server 2005 system-supplied data types. These include xml, bigint, and sql_variant. There is no implicit conversion on assignment from the sql_variant data type, but there is implicit conversion to sql_variant.

Data type conversion table
ms187928.note(en-US,SQL.90).gifNote:
Because Unicode data always uses an even number of bytes, use caution when you convert binary or varbinary to or from Unicode supported data types. For example, the following conversion does not return a hexadecimal value of 41; it returns 4100: SELECT CAST(CAST(0x41 AS nvarchar) AS varbinary).

Large-value Data Types

Large-value data types exhibit the same implicit and explicit conversion behavior as their smaller counterparts, specifically the varchar, nvarchar and varbinary data types. However, you should consider the following guidelines:

  • Conversion from image to varbinary(max) and vice-versa is an implicit conversion, and so are conversions between text and varchar(max), and ntext and nvarchar(max).
  • Conversion from large-value data types, such as varchar(max), to a smaller counterpart data type, such as varchar, is an implicit conversion, but truncation will occur if the large value is too big for the specified length of the smaller data type.
  • Conversion from varchar, nvarchar, or varbinary to their corresponding large-value data types is performed implicitly.
  • Conversion from the sql_variant data type to the large-value data types is an explicit conversion.
  • Large-value data types cannot be converted to the sql_variant data type.

For information about converting Microsoft .NET Framework common language runtime (CLR) user-defined types, see Performing Operations on User-defined Types. For more information about converting from the xml data type, see Generating XML Instances.

xml Data Type

When you explicitly or implicitly cast the xml data type to a string or binary data type, the content of the xml data type is serialized based on a set of rules. For information about these rules, see Serialization of XML Data. For information on how to cast from XML to a CLR user-defined type, see Performing Operations on User-defined Types. For information about converting from other data types to the xml data type, see Generating XML Instances.

text and image Data Types

Automatic data type conversion is not supported for the text and image data types. You can explicitly convert text data to character data, and image data to binary or varbinary, but the maximum length is 8000 bytes. If you try an incorrect conversion such as trying to convert a character expression that includes letters to an int, SQL Server returns an error message.

Output Collation

When the output of CAST or CONVERT is a character string, and the input is a character string, the output has the same collation and collation label as the input. If the input is not a character string, the output has the default collation of the database, and a collation label of coercible-default. For more information, see Collation Precedence (Transact-SQL).

To assign a different collation to the output, apply the COLLATE clause to the result expression of the CAST or CONVERT function. For example:

SELECT CAST('abc' AS varchar(5)) COLLATE French_CS_AS

Truncating and Rounding Results

When you convert character or binary expressions (char, nchar, nvarchar, varchar, binary, or varbinary) to an expression of a different data type, data can be truncated, only partially displayed, or an error is returned because the result is too short to display. Conversions to char, varchar, nchar, nvarchar, binary, and varbinary are truncated, except for the conversions shown in the following table.

From data type To data type Result
int, smallint, or tinyint char *
varchar *
nchar E
nvarchar E
money, smallmoney, numeric, decimal, float, or real char E
varchar E
nchar E
nvarchar E

* = Result length too short to display. E = Error returned because result length is too short to display.

SQL Server guarantees that only roundtrip conversions, conversions that convert a data type from its original data type and back again, will yield the same values from version to version. The following example shows such a roundtrip conversion:

DECLARE @myval decimal (5, 2)
SET @myval = 193.57
SELECT CAST(CAST(@myval AS varbinary(20)) AS decimal(10,5))
-- Or, using CONVERT
SELECT CONVERT(decimal(10,5), CONVERT(varbinary(20), @myval))
ms187928.note(en-US,SQL.90).gifNote:
Do not try to construct binary values and then convert them to a data type of the numeric data type category. SQL Server does not guarantee that the result of a decimal or numeric data type conversion to binary will be the same between versions of SQL Server.

The following example shows a resulting expression that is too small to display.

USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName, SUBSTRING(c.Title, 1, 25) AS Title, CAST(e.SickLeaveHours AS char(1)) AS 'Sick Leave'
FROM HumanResources.Employee e JOIN Person.Contact c ON e.EmployeeID = c. ContactID
WHERE NOT EmployeeID >5

Here is the result set.

FirstName      LastName         Title                  Sick Leave
---------      ---------      -------------------   -----------
Gustavo        Achong         Mr.                   *
Catherine      Abel           Ms.                   *
Kim            Abercrombie    Ms.                   *
Humberto       Acevedo        Sr.                   *
Pilar          Ackerman       Sra.                  *

(5 row(s) affected)

When you convert data types that differ in decimal places, sometimes the result value is truncated and at other times it is rounded. The following table shows the behavior.

From To Behavior
numeric numeric Round
numeric int Truncate
numeric money Round
money int Round
money numeric Round
float int Truncate
float numeric Round
float datetime Round
datetime int Round

For example, the result of the following conversion is 10:

SELECT CAST(10.6496 AS int)

When you convert data types in which the target data type has fewer decimal places than the source data type, the value is rounded. For example, the result of the following conversion is $10.3497:

SELECT CAST(10.3496847 AS money)

SQL Server returns an error message when nonnumeric char, nchar, varchar, or nvarchar data is converted to int, float, numeric, or decimal. SQL Server also returns an error when an empty string (” “) is converted to numeric or decimal.

Converting Binary String Data

When binary or varbinary data is converted to character data and an odd number of values is specified following the x, SQL Server adds a 0 (zero) after the x to make an even number of values.

Binary data is made up of the characters from 0 through 9 and from A through F, or from a through f, in groups of two characters each. Binary strings must be preceded by 0x. For example, to input FF, type 0xFF. The maximum value is a binary value of 8000 bytes, each of which is FF. The binary data types are not for hexadecimal data but instead for bit patterns. Conversions and calculations of hexadecimal numbers stored as binary data can be unreliable.

When you specify the length of a binary data type, every two characters count as one. A length of 10 signifies that 10 two-character groupings will be entered.

Empty binary strings are represented by 0x and can be stored as binary data.

A. Using both CAST and CONVERT

Each example retrieves the name of the product for those products that have a 3 in the first digit of their list price and converts their ListPrice to int.

-- Use CAST
USE AdventureWorks;
GO
SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice
FROM Production.Product
WHERE CAST(ListPrice AS int) LIKE '3%';
GO

-- Use CONVERT.
USE AdventureWorks;
GO
SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice
FROM Production.Product
WHERE CONVERT(int, ListPrice) LIKE '3%';
GO

B. Using CAST with arithmetic operators

The following example calculates a single column computation (Computed) by dividing the total year-to-date sales (SalesYTD) by the commission percentage (CommissionPCT). This result is converted to an int data type after being rounded to the nearest whole number.

USE AdventureWorks;
GO
SELECT CAST(ROUND(SalesYTD/CommissionPCT, 0) AS int) AS 'Computed'
FROM Sales.SalesPerson
WHERE CommissionPCT != 0;
GO

Here is the result set.

Computed
------
379753754
346698349
257144242
176493899
281101272
0
301872549
212623750
298948202
250784119
239246890
101664220
124511336
97688107

(14 row(s) affected)

C. Using CAST to concatenate

The following example concatenates noncharacter, nonbinary expressions by using CAST.

USE AdventureWorks;
GO
SELECT 'The list price is ' + CAST(ListPrice AS varchar(12)) AS ListPrice
FROM Production.Product
WHERE ListPrice BETWEEN 350.00 AND 400.00;
GO

Here is the result set.

ListPrice
------------------
The list price is 357.06
The list price is 364.09
The list price is 364.09
The list price is 364.09
The list price is 364.09

(5 row(s) affected)

D. Using CAST to produce more readable text

The following example uses CAST in the select list to convert the Name column to a char(10) column.

USE AdventureWorks;
GO
SELECT DISTINCT CAST(p.Name AS char(10)) AS Name, s.UnitPrice
FROM Sales.SalesOrderDetail s JOIN Production.Product p on s.ProductID = p.ProductID
WHERE Name LIKE 'Long-Sleeve Logo Jersey, M';
GO

Here is the result set.

Name       UnitPrice
---------- ---------------------
Long-Sleev 31.2437
Long-Sleev 32.4935
Long-Sleev 49.99

(3 row(s) affected)

E. Using CAST with the LIKE clause

The following example converts the money column SalesYTD to an int and then to a char(20) column so that it can be used with the LIKE clause.

USE AdventureWorks;
GO
SELECT p.FirstName, p.LastName, s.SalesYTD, s.SalesPersonID
FROM Person.Contact p JOIN Sales.SalesPerson s ON p.ContactID = s.SalesPersonID
WHERE CAST(CAST(s.SalesYTD AS int) AS char(20)) LIKE '2%';
GO

Here is the result set.

FirstName        LastName            SalesYTD         SalesPersonID
---------------- ------------------- ---------------- -------------
Carol            Elliott             2811012.7151      279
Julie            Estes               219088.8836       288
Janeth           Esteves             2241204.0424      289

(3 row(s) affected)

F. Using CONVERT or CAST with typed XML

The following are several examples that show using CONVERT to convert to typed XML by using the xml data type.

This example converts a string with white space, text and markup into typed XML and removes all insignificant white space (boundary white space between nodes):

CONVERT(XML, '<root><child/></root>')

This example converts a similar string with white space, text and markup into typed XML and preserves insignificant white space (boundary white space between nodes):

CONVERT(XML, '<root>          <child/>         </root>', 1)

This example casts a string with white space, text, and markup into typed XML:

CAST('<Name><FName>Carol</FName><LName>Elliot</LName></Name>'  AS XML)

For more examples, see Generating XML Instances.

G. Using CAST and CONVERT with datetime data

The following example displays the current date and time, uses CAST to change the current date and time to a character data type, and then uses CONVERT display the date and time in the ISO 8901 format.

SELECT
   GETDATE() AS UnconvertedDateTime,
   CAST(GETDATE() AS nvarchar(30)) AS UsingCast,
   CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601  ;
GO

Here is the result set.

UnconvertedDateTime     UsingCast                      UsingConvertTo_ISO8601

----------------------- ------------------------------ ------------------------------

2006-04-18 09:58:04.570 Apr 18 2006 9:58AM            2006-04-18T09:58:04.570

(1 row(s) affected)

The following example is approximately the opposite of the previous example. The example displays a date and time as character data, uses CAST to change the character data to the datetime data type, and then uses CONVERT to change the character data to the datetime data type.

SELECT
   '2006-04-04T15:50:59.997' AS UnconvertedText,
   CAST('2006-04-04T15:50:59.997' AS datetime) AS UsingCast,
   CONVERT(datetime, '2006-04-04T15:50:59.997', 126) AS UsingConvertFrom_ISO8601 ;
GO

Here is the result set.

UnconvertedText         UsingCast               UsingConvertFrom_ISO8601

----------------------- ----------------------- ------------------------

2006-04-04T15:50:59.997 2006-04-04 15:50:59.997 2006-04-04 15:50:59.997

(1 row(s) affected)

Release History
17 July 2006
New content:
  • Added example G.
14 April 2006
New content:
  • Added description of the Z time zone indicator for style 127.

Tags What's this?: cast (x) convert (x) tsql (x) Add a tag

Yazı kategorisi: Cast, Convert, Transact-SQL | » yorum bırak;

Convert DateTime

Yazan: esersahin 13/03/2009

CREATE PROCEDURE sproc_UpdateTemp_Sub
(
@PreviousDay datetime,
@PresentDay datetime
)
AS

select * from table1 where date between @previousday and @presentday

Go

CREATE PROCEDURE sproc_UpdateTemp
AS

DECLARE @PreviousDay datetime
DECLARE @PresentDay datetime

select @PresentDay = convert( datetime, convert( varchar(10), getdate(), 101 ))
select @PreviousDay = dateadd( day, -1, @PresentDay )

exec sproc_UpdateTemp_Sub @PreviousDay, @PresentDay

Go

Declare @presentday as varchar(10)
SET @PresentDay =  convert( varchar(10), getdate(), 101 )
select * from table where Convert( varchar(10), date1, 101)  = @presentday

Why are you using varchar instead of datetime datatype? If it is just to lose the time part of GetDate it is much more efficient convert it using a float.

Something like

select cast(floor(convert( float,getdate())) as datetime)

returns you today’s date with the time part set to 00:00:00.

You could create a UDT like this:

create function dbo.fnDateFromDateTime(@da

tetime datetime)
returns datetime
AS
begin
return cast(floor(convert( float,@datetime)) as datetime)
end

And call it like this:

select dbo.fnDateFromDateTime(GetDate())

Yazı kategorisi: Cast, Convert, DateTime, SQL, Sql Server, Transact-SQL | » yorum bırak;