| Author |
Topic  |
|
|
cms9651
Starting Member
26 Posts |
Posted - 03/05/2013 : 04:19:27
|
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
47091 Posts |
Posted - 03/05/2013 : 04:35:02
|
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 |
 |
|
|
cms9651
Starting Member
26 Posts |
Posted - 03/05/2013 : 04:49:36
|
thank you for help. your query not working... the output is null the values of [myDateString] is 31/12/2012 09:01SELECT
*
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 |
 |
|
|
cms9651
Starting Member
26 Posts |
Posted - 03/05/2013 : 05:11:59
|
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.
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47091 Posts |
Posted - 03/05/2013 : 05:13:14
|
use it as suggested. I've not applied any SUBSTRING . copy and paste it as given
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
cms9651
Starting Member
26 Posts |
Posted - 03/05/2013 : 05:17:49
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47091 Posts |
Posted - 03/05/2013 : 05:18:25
|
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/
|
 |
|
|
cms9651
Starting Member
26 Posts |
Posted - 03/05/2013 : 05:42:40
|
| thanks a lot! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47091 Posts |
Posted - 03/05/2013 : 06:06:44
|
wc
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|
|
|