SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Extract only the last three days
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cms9651
Starting Member

28 Posts

Posted - 03/05/2013 :  04:19:27  Show Profile  Reply with Quote
Extract only the last three days

hi all, hope in your help.

this is my procedure for export in txt file the values of the table in db sql server 2008.

the table is on a remote server and the field [myDateString] is nvarchar 255 and I'm not admin.

I need extract only the last three days for the table, in this moment extract all current year.

Can you help me ?
thank you
EXEC master.dbo.sp_configure 'show advanced options',
 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell',
 1 RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT * FROM tbl WHERE YEAR(CAST(SUBSTRING([myDateString], 7, 4) AS DATETIME)) = 2013;"
   queryout "\\myserver\public\tkt.txt" -T -c -t;'

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/05/2013 :  04:35:02  Show Profile  Reply with Quote
make it like


EXEC master.dbo.sp_configure 'show advanced options',
 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell',
 1 RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT * FROM tbl WHERE CAST([myDateString] AS datetime)> = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-3) 
AND CAST([myDateString] AS datetime) < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1) ;"
   queryout "\\myserver\public\tkt.txt" -T -c -t;'


I hope your [myDateString] field is having values in valid date format

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Edited by - visakh16 on 03/05/2013 04:35:57
Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 03/05/2013 :  04:49:36  Show Profile  Reply with Quote
thank you for help.
your query not working... the output is null
the values of [myDateString] is 31/12/2012 09:01
SELECT
	*
FROM
	tbl
WHERE
	(
		CAST (
			SUBSTRING ([myDateString], 7, 4) AS DATETIME
		) > = DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()) ,- 3
		)
		AND CAST (
			SUBSTRING ([myDateString], 7, 4) AS datetime
		) < DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()),
			1
		));

Edited by - cms9651 on 03/05/2013 04:52:10
Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 03/05/2013 :  05:11:59  Show Profile  Reply with Quote
If tried:

[SQL] SELECT
	*
FROM
	tbl
WHERE
	(
		CAST (
			[myDateString] AS DATETIME
		) > = DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()) ,- 3
		)
		AND CAST (
			[myDateString] AS datetime
		) < DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()),
			1
		))
[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/05/2013 :  05:13:14  Show Profile  Reply with Quote
use it as suggested. I've not applied any SUBSTRING . copy and paste it as given

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 03/05/2013 :  05:17:49  Show Profile  Reply with Quote
Ok, but:

[SQL] SELECT
	*
FROM
	tbl
WHERE
	(
		CAST (
			[myDateString] AS DATETIME
		) > = DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()) ,- 3
		)
		AND CAST (
			[myDateString] AS datetime
		) < DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()),
			1
		))
[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/05/2013 :  05:18:25  Show Profile  Reply with Quote
quote:
Originally posted by cms9651

If tried:

[SQL] SELECT
	*
FROM
	tbl
WHERE
	(
		CAST (
			[myDateString] AS DATETIME
		) > = DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()) ,- 3
		)
		AND CAST (
			[myDateString] AS datetime
		) < DATEADD(
			dd,
			DATEDIFF(dd, 0, GETDATE()),
			1
		))
[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.



Thats the problem with not using proper datatype for fields.

make it like this and try


EXEC master.dbo.sp_configure 'show advanced options',
 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell',
 1 RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT * FROM tbl WHERE CONVERT(datetime,[myDateString],103)> = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-3) 
AND CONVERT(datetime,[myDateString],103) < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1) ;"
   queryout "\\myserver\public\tkt.txt" -T -c -t;'


also make sure you read this

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cms9651
Starting Member

28 Posts

Posted - 03/05/2013 :  05:42:40  Show Profile  Reply with Quote
thanks a lot!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/05/2013 :  06:06:44  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000