SQL Server de-weight and judge whether it is a number

Source: Internet
Author: User

The SQL statement query is stripped of duplicates:

Use distinct to remove duplicates:

First you can clearly see the duplicate name, then let's try to use distinct to weigh it.

1   Select distinct *  from Student

Hey, why didn't it work? It turnsout that distinct is heavy based on the field being queried . * represents all fields, can be clearly seen field s_id and S_stuno is not possible to have duplicates, because a primary key and self-growing identity column, a unique (unique) constraint.

Now that you understand this, let's go to the following three fields of S_name,s_sex,s_height:

1   Select distinct  from Student

SQL determines whether a string is a number:

Method One: Use the SQL Server built-in function IsNumeric ():

1   Select IsNumeric('123')--returns 12   Select IsNumeric('123.12')--returns 13   Select IsNumeric('a123.12')--returns 04   Select IsNumeric('ABC')--returns 0

The function IsNumeric () returns a result of 1 for all numbers, and a result of 0 for other characters with the exception of numbers, but "." (The decimal point is also considered a part of it.)

Method two: Match strings with characters other than numbers (the decimal point counts as other characters)

1 if(('123'  not  like '%[^0-9]%'))--Digital2 begin3     Select 'Digital'4 End5 Else6 begin7     Select 'Non-digital'8 End9 Ten if(('A123'  not  like '%[^0-9]%'))--Non-digital One begin A     Select 'Digital' - End - Else the begin -     Select 'Non-digital' - End -  + if(('123.1'  not  like '%[^0-9]%'))--Non-digital - begin +     Select 'Digital' A End at Else - begin -     Select 'Non-digital' - End

Of course, we can also define a custom function in this way, which we'll say later.

SQL queries for a value that has the most number of occurrences of a field:

1 -- queries the name (S_NAME) in the table Student most occurrences 2   Select Top 1 s_name,COUNT(1fromgroupbyorderby  COUNT(1desc

SQL Server [case-then-else end]:

1 SelectS_stuno number, s_name name,2  CaseS_sex when 'male'  Then 'Boys'  when 'female'  Then 'Schoolgirl' Else 'Confidentiality' EndGender--Way One3  Case  whenS_sex='male'  Then 'Boys'  whenS_sex='female'  Then 'Schoolgirl' Else 'Confidentiality' EndGender--Mode two4 s_height Height5  fromStudent

SQL object_id, object_name, object_definition:

1 if(object_id('Student') is  not NULL)--determine if there is a Student this table2 begin3     Select 'table already exists'4 End5 Else6 begin7     Select 'table does not exist, please create'8 End9 Ten --Specify a temporary table name, you must precede the temporary table name with the database name, for example: One IF object_id('tempdb: #table1') is  not NULL        --determine if the temporary table table1 exists, A Select 'table already exists' - Else - Select 'table does not exist, please create' the  - Select object_id('Student')--returns 245575913 returns the object ID based on the object name -  - Select object_name('245575913')--returns Student returns the object name based on the object ID +  - SelectObject_definition ('245575913')--returns the source file for the object +  A SelectObject_definition (object_id('Student'))
 object_id Syntax: object_id (' [database_name. [Schema_name]. | Schema_name. ] object_name ' [, ' object_type '])
Parameters:
' object_name ' (required)
is the object to use. object_name is a varchar or nvarchar. If object_name is a varchar, it is implicitly converted to nvarchar. You can choose whether to specify a database and schema name.
' object_type ' (optional)
the schema-scoped object type. Object_type is a varchar or nvarchar. If object_type is a varchar, it is implicitly converted to nvarchar.
return type:
return int
PS: For spatial indexes, OBJECT_ID returns NULL. When an error occurs, NULL is returned.
object_name Syntax: object_name (object_id [, database_id])
Parameters:
' object_id ' (required)
The ID of the object to use. OBJECT_ID is an int that is assumed to be a schema-scoped object in the specified database, or in the current database context.
' database_id ' (optional)
The ID of the database in which to find the object. database_id is int.
return type:
return nvarchar (max)
PS: NULL is returned when an error occurs or when the caller does not have permission to view the object. If the Auto_Close option for the target database is set to ON, this function opens the database.
users can only view metadata for securable objects that are owned by the user or have been granted permission to the securable object.
That is , if the user does not have any permissions on the object, the built-in functions that would have narimoto data (such as object_name) might return NULL.
object_definition Syntax: Object_definition (object_id)
Parameters:
' object_id ' (required)
The ID of the object to use. OBJECT_ID is an int, and is assumed to represent an object in the context of the current database.
return type:
return nvarchar (max)
PS: NULL is returned when an error occurs or when the caller does not have permission to view the object. See msdn:https://msdn.microsoft.com/zh-cn/library/ms176090.aspx

SQL Server de-weight and judge whether it is a number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.