Solved

Uisng bcp utility to copy data from sql to file and vice versa

Posted on 2004-09-15
17
490 Views
Last Modified: 2007-12-19
Hi guys, hope you all are doing fine, guys I am having a trouble I am using the bcp utility to copy the data from one table to a text file but I just wanted to give two date parameters in that so that the data withing those date parameters should come and should be copied to the text file but I am not bale to get the syntax right. ia m using the below mentioned command


exec master..xp_cmdshell 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and '+@dt2 '" queryout "c:\test.txt" -c -q -U"username" -P"password"'

I have declared the @dt1 and @dt2 parameters.

Please help me out urgently.
0
Comment
Question by:faisal_n_k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
17 Comments
 
LVL 11

Expert Comment

by:ram2098
ID: 12062303
It is always good to write it like this

declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and '+@dt2 ' + ' " queryout "c:\test.txt" -c -q -U"username" -P"password"'


exec master..xp_cmdshell @sqlquery
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062316
I guess one more syntax error....

declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"username" -P"password"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062394
Thanks ram for the comments but I ran the command it is giving me an error. The command that I ran is below


declare @dt1 as datetime, @dt2 as datetime
Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery



It gives me an error like

Server: Msg 241, Level 16, State 1, Line 6
Syntax error converting datetime from character string.

Thanks for helping
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:ram2098
ram2098 earned 30 total points
ID: 12062428
Declare both of them as varchar..I guess that will work

declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062491
But ram it is giving me ) rows copied whereas the data is there
0
 

Author Comment

by:faisal_n_k
ID: 12062508
0 rows copied and neither it si giving me any error
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062566
Can you try the following..........to avoid any date formatting issues


declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+cast(varchar(30),@dt1,101)+' and ' + cast(varchar(30),@dt2,101)  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062579
ram, with cast it is giving that varchar is not a recognized function and when I change cast to convert it is still saying 0 rows copied.
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062588
Sorry..I gave you wrong syntax..

declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '08/01/2004'
Set @dt2 = '08/10/2004'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062600
still 0 rows copied my dear friend, please help me out with this
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062634
Are you getting any rows if you execute the following query..through query analyzer?

select * from Watch.dbo.Dgattendance where logindate between '08/01/2004' and '08/10/2004'

If it is returning rows, I haven't seen anything wrong with the BCP command.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 20 total points
ID: 12063217
I think that ram has been close to the solution, but assumes that the field LOGINDATE is indeed stored as datetime. If it is NOT (but as varchar), then the solution will be different, please tell us.

declare @dt1 as datetime, @dt2 as datetime
Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

set @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between CONVERT(datetime, '''+ CONVERT( varchar(20), @dt1 , 101 ) +''', 101) and CONVERT(datetime, ''' + CONVERT(varchar(20), @dt2 , 101 ) + ''', 101) " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'
EXEC ( @sqlquery )

This should work for ANY regional setting you have.

CHeers
0
 

Author Comment

by:faisal_n_k
ID: 12071894
Thanks angel you have been really an angel for me thanks again for the help it has worked but ram has also helped me out but I am confused for the points whom should I give the points
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12072230
You can actually split the points between both of us....

Nice to see your problem solved...:)
0
 

Author Comment

by:faisal_n_k
ID: 12072307
How do we split the points
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12073023
see the link below..it helps you on this........

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi19
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 28
Moving away from Access 2003 adp files 4 41
Report 8 21
t-sql left join 2 20
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question