|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:01 AM
Points: 42,
Visits: 121
|
|
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;'
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 6:13 PM
Points: 712,
Visits: 1,053
|
|
Looks like your date data is stored as a string instead of a date data type. Can you post up some of the date string values?
The current query is finding the 7th character in the myDateString string, then grabbing the next 4 characters to use as a year string value, then converting it to a datetime data type. The year function gets the year value as an integer to compare to the 2013 integer on the right of the = sign.
SELECT * FROM tbl WHERE YEAR(CAST(SUBSTRING([myDateString], 7, 4) AS DATETIME)) = 2013; Depending on what else is stored in the string, you could just use CONVERT(datetime, myDateString,103) > 3 days ago (getdate - 3)
SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(day, -3, getdate())
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 6:13 PM
Points: 712,
Visits: 1,053
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:01 AM
Points: 42,
Visits: 121
|
|
thank you, but:
[SQL] SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(day, -3, getdate())
[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.
the value of [myDateString] is e.g. 31/12/2012 09:01
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 6:13 PM
Points: 712,
Visits: 1,053
|
|
Painful dealing with dates stored as strings..
You'll have to use the same method as the YEAR query.
SELECT * FROM tbl WHERE CAST(SUBSTRING([myDateString], 1, 10) AS DATETIME) >= DATEADD(d, -3, getdate());
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:01 AM
Points: 42,
Visits: 121
|
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 6:13 PM
Points: 712,
Visits: 1,053
|
|
Oh just noticed I gave you the wrong syntax for that first reply (DAY instead of d)
[SQL] SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(d, -3, getdate())
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:01 AM
Points: 42,
Visits: 121
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Thursday, May 23, 2013 9:27 AM
Points: 5,618,
Visits: 10,990
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:01 AM
Points: 42,
Visits: 121
|
|
| Anything since 72 hours ago from now.
|
|
|
|