Posts

Showing posts from February, 2010

Get the Server Name in SQL Server

Image
We can get the Connected Sql Server Name using the Built-in function SQL Server as shown below,

Extract only the Date from DateTime in SQL Server

Normaly extarcting date only from DATETIME is commaon and serious problem in SQL SERVER. Here i have given some common methods to extract on the date from datetime Method 1 ------------ SELECT CAST ( FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME ) Method 2--but it is not correct ------------------------------------ This is similar to first method but we have used INT instead of FLOAT . But it will seldom correct. It will not give you the exact date of what you expecting. So better not to use INT. SELECT CAST ( FLOOR(CAST(GETDATE() AS INT)) AS DATETIME ) Method 3 ---------- i would recommend this method to extract inly date though there are many methods. because this method is very simple and easy. SELECT CAST ( STR(DAY(GETDATE()))+ '/' + STR(MONTH(GETDATE())) +'/'+ STR(YEAR(GETDATE())) AS DATETIME ) Method 4 ---------- In this we have used CONVERT function with 113 date type. SELECT CONVERT(DATETIME, CONVERT(VARCHAR(12), GETDATE(), 113)) Method 5 ---------- Here we have u

PIVOT() Function MSSQL Server

Image
Introduction The PIVOT() function in MS SQL Server is very useful function. This is works like Cross-Tab table in Crystal Report. Explanation For an instance, as shown below table, we have customer,Yr,Mothname, and TTL columns. The Year column consisting distinct records 2009,2010 year. If you want to show the Year data as each column header name, then you will have to use the PIVOT() function to show the distinct records as column header. SELECT user_id Customer,datepart(“yyyy”,order_date) Yr,datename(month,order_date) [Month] , isnull(sum(total_value),0) TTL FROM tbl_po_master group by user_id,datepart(“yyyy”,order_date),datename(month,order_date) Using PIVOT() function In PIVOT() function, you must specify the column values as shown below, select * from (SELECT user_id Customer,datepart(“yyyy”,order_date) Yr,datename(month,order_date) [Month] , isnull(sum(total_value),0) TTL FROM tbl_po_master group by user_id,datepart(“yyyy”,order_date),datename(month,order_date) ) s PIVOT ( sum(

How to insert multiple rows records using a single Insert Into SQL Command

Image
if we want to insert multiple rows records, we can still use insert into command for each row as shown below, but you can insert all those rows by using one insert into command rather than using more insert into command as shown below,