Showing posts with label SQL Server Tutorial. Show all posts
Showing posts with label SQL Server Tutorial. Show all posts

Tuesday, 21 August 2018

SQL Server to text/CSV file export

There are several to export data from SQL server to text file. There are more than 8 way to export data from SQL server to a csv file.We will discuss here , three easy way to export data from SQL server to csv file.

Export query result to the external  csv file.
1)Open query analyzer, write your query.


2)From the menu, choose tool menu, select option.

3)A new window will be open. 

4)Select Query Result.

5)From the drop down select "Result to file ",then set location of your text file.



Now executive query. Now open your file, you will see that the result set is in you file.




Export table data
You can export the whole table data to csv. 


1)From the object Explorer, select database.Right click on database,a context menu will appear.Select Task>>Export Data.



 2)A new window will appear.Click on "Next".




3)Choose your authentication , then click on "Next".





4)Select Destination "Flat File Destination". Select "File Name" by Browsing your target file.then click on "Next"






5) Click on Next.




SQL Bulk copy
 

SQL server have a smart way to export data to the text file. SQL bulk copy is a good option to export data to external text file with very fast and accurate manner. Exporting data one by one row  is very time consuming a slow process. SQL bulk copy, copy data as a bulk  and write to file as a bulk. "bcp" is the comment used for bulk copy of data.


Below is the example of SQL bulk copy. We have created a table called "
tbl_bulkcopy" and insert data into it.A text file is created called "bulkcopy.txt". Now using "bcp" command to export the data to the text file.

--1. we are createing a table first
CREATE table tbl_bulkcopy
(
id    INT identity,
name  VARCHAR(50),
roll  INT
)
--insert data to the table
INSERT INTO tbl_bulkcopy(name,roll) VALUES('Ayan',20)
INSERT INTO tbl_bulkcopy(name,roll) VALUES('John',21)

--declare some use ful variable
DECLARE @fp             NVARCHAR(200)
DECLARE @fn             NVARCHAR(200)
DECLARE @sql            NVARCHAR(200)
DECLARE @opqry            NVARCHAR(200)

--set values of the variable
SELECT @fp ='C\copypath\'
SELECT @fn ='bulkcopy.txt'
SELECT @opqry='type '+@fp+@fn
---
---write the select query
SELECT @sql='SELECT name ,roll FROM tbl_bulkcopy'
---create the bulk copy script
SET @sql =  'bcp ' + '"' +  @sql + '"' +  '  queryout "' + @fp + @fn +'"   -c -q -C1252 -T '
--run the script in command shell
EXECUTE master..xp_cmdshell @sql
--
--to see the output of the file , please write the code
--or you can open the file directly
EXECUTE master..xp_cmdshell @opqry













Monday, 20 August 2018

SQL Server Date Format

SQL server is used widely all over the world. In different country and different places, date format are different. For example in India it is used day month year format (31/03/2018) but in US it is used month day year(03/31/2018) format. When SQL server returned results, developer have to write code in such a way, that date format should be compatible for that country and region. 

         SQL Server have no such regular expression to express the date format.However , there is some some indicator that return date in a specific style format. Below is the example. This example so how the number 100-113  return different format of dates and Time. This format does not return date only, it is a combination of date and time. You have to  subtract date part  of the output.Bellow  how the extraction is done to extract date part.


    101    mm/dd/yyyy    US
    102    yyyy.mm.dd    ANSI
    103    dd/mm/yyyy    British/French
    104    dd.mm.yyyy    German
    105    dd-mm-yyyy    Italian
    106    dd mon yyyy   -
    107    Mon dd, yyyy  -
    108    hh:mm:ss      -
    109    mon dd yyyy   hh:mi:ss:mmmAM 
    110    mm-dd-yyyy    USA
    111    yyyy/mm/dd    Japan
    112    yyyymmdd      ISO
    113    dd mon yyyy hh:mi:ss:mmm    Europe (24 hour clock)>
     

Todays is 20th August 2018.I am creating the article.Now sql server return the following code.


Example

SELECT CONVERT(varchar, GETDATE(), 100)
Output : Aug 20 2018 12:01PM
 
SELECT CONVERT(varchar, GETDATE(), 101)
Output : 08/20/2018
 
SELECT CONVERT(varchar, GETDATE(), 102)
Output : 2018.08.20
 
SELECT CONVERT(varchar, GETDATE(), 103)
Output : 20/08/2018
 
SELECT CONVERT(varchar, GETDATE(), 104)
Output : 20.08.2018
 
SELECT CONVERT(varchar, GETDATE(), 105)
Output : 20-08-2018

SELECT CONVERT(varchar, GETDATE(), 106)
Output : 20 Aug 2018

SELECT CONVERT(varchar, GETDATE(), 107)
Output : Aug 20, 2018

SELECT CONVERT(varchar, GETDATE(), 108)
Output : 12:02:59

SELECT CONVERT(varchar, GETDATE(), 109)
Output :  Aug 20 2018 12:03:12:070PM

SELECT CONVERT(varchar, GETDATE(), 110)
Output : 08-20-2018

SELECT CONVERT(varchar, GETDATE(), 111)
Output : 2018/08/20

SELECT CONVERT(varchar, GETDATE(), 112)
Output : 20180820

SELECT CONVERT(varchar, GETDATE(), 113)
Output : 20 Aug 2018 12:03:40:220 


Below are list of more date format available in Sql Server

    1    mm/dd/yy   
    2    yy.mm.dd
    3     yy.mm.dd
    4    dd/mm/yyyy
    5    dd-mm-yy  
    6    dd MM yy   
    7    MM dd yy
    8    Time   
    9     MM dd yyyy +Time   
    10    dd/mm   
    11   mm/dd/yyyy   
    12    yymmdd   
    13    dd MM yyyy +Time  
    14   mm/dd/yyyy    


Example
 
SELECT CONVERT(varchar, GETDATE(), 1) 
Output : 08/20/18


SELECT CONVERT(varchar, GETDATE(), 2)
Output : 18.08.20


SELECT CONVERT(varchar, GETDATE(), 3) 
Output : 20/08/18

 dd/mm/yy
SELECT CONVERT(varchar, GETDATE(), 4)
Output : 20.08.18

 dd.mm.yy
SELECT CONVERT(varchar, GETDATE(), 5)
Output : 20-08-18

dd-mm-yy
SELECT CONVERT(varchar, GETDATE(), 6)
Output : 20 Aug 18

dd MM yy
SELECT CONVERT(varchar, GETDATE(), 7)
Output : Aug 20, 18

MM dd yy
SELECT CONVERT(varchar, GETDATE(), 8)
Output :  13:20:29


SELECT CONVERT(varchar, GETDATE(), 9) 
Output : Aug 20 2018 1:20:46:377PM

MM dd yyyy +Time
SELECT CONVERT(varchar, GETDATE(), 10) 
Output : 08-20-18

mm-dd-yy
SELECT CONVERT(varchar, GETDATE(), 11)
Output :  18/08/20
 dd/mm
SELECT CONVERT(varchar, GETDATE(), 12) 
Output : 180820


yymmdd
SELECT CONVERT(varchar, GETDATE(), 13)
Output :  20 Aug 2018 13:21:32:553

dd MM yyyy +Time
SELECT CONVERT(varchar, GETDATE(), 14)
Output :  13:21:32:553


বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...