Ir al contenido principal

SQL Server - Tips

List All Tables of Database

 


USE [YourDBName]
GO 

SELECT 
*FROM sys.Tables
GO


Insert structure from one table to another


SELECT *
into new_table
FROM origin_table where 1 =2 

How to create a counter

Use the function ROW_NUMBER ( ), where the syntaxis is
 
ROW_NUMBER ( ) OVER ( FIELD order_by_clause )
 
Where FIELD  is the name of the column you want to use to start counting. Example:

SELECT ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS Row, 
FROM Sales.vSalesPerson
 

Enumerate rows ... 1.2.3....

 
ROW_NUMBER() over ( ORDER BY [Job Stage]) as [pyramidPosition]
 

Reset the enumeration  ..... 1.2.3....1.2.3

 

ROW_NUMBER() over (partition BY idProject ORDER BY [Job Stage]) as [pyramidPosition]
 
http://sqlfiddle.com/#!6/501c0/2/0
 
 Update Table from Select
 

Update projects

set projects.tg4Real = [TG4 Date Real DD/MM/YYYY] ,
 projects.tg5Real = [TG5 Date Real DD/MM/YYYY] 
from
 projects p
inner join
 gtt...[Upload$] u
on p.projectName = [Project Name]
 
 

Multi user

ALTER DATABASE DATA_BASE SET MULTI_USER; 
 

Add user to a DB

Can you check it by Going To Security --> Login-->Right and Click Property and then goto user mapping tab. Then select the database
 
 

Convert String Dates to Date/Datetime 

From '12/10/25' to date 
CONVERT(DATE,CAST ( RTRIM( LTRIM([CREATION DATE])) as nvarchar), 103) 

From '16/01/2007 3:03:35:270AM' to datetime
From '23/06/2014 09:07:17 a. m.'  to datetime
CONVERT(datetime, ( RIGHT(LEFT([START TIME],5),2) + '/'+LEFT([START TIME],2)+'/'+ RIGHT(LEFT([START TIME],10),4)+  SUBSTRING([START TIME],11, 9 ) + REPLACE( SUBSTRING([START TIME],21, 4 ), '. ', '' ) ) , 120 ) as ODBC_datetime


 Add many columns in one

Function:  STUFF

select distinct t1.userId, t1.userName,
  STUFF((SELECT ', ' + t2.vpn 
         from Request t2
         where t1.userId = t2.userId
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,0,'') VPN,
        t1.managerName, t1.managerUserId, t1.ccName, t1.ccUserId
from Request t1
order by [userId]




Substract Dates

DATEDIFF( minute, [resto_rst_prov_date], crea_date))


Days with decimals

DATEDIFF( minute, [resto_rst_prov_date], crea_date))/(60*24.0)

Days with two decimals


convert(decimal(12,2), (DATEDIFF( minute, [resto_rst_prov_date], crea_date))/(60*24.0)) 

 

 

 

 

 

 

 


Comentarios

Entradas populares de este blog

C# Using tabs

To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

Rails - Basic Steps III

pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

Python create package

Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...