[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-09-15
17
Medium Priority
?
494 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Assisted Solution

by:ram2098
ram2098 earned 120 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 80 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

649 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