views:

91

answers:

3

I've got this field in my database year_start_1 and it is an integer field and an example of an ouput is 20100827 I'm trying to create a substring to create year, week, day and change the format to be 27/08/2010

Here's what i'm trying

Dim query as String = "Select * from openquery (devbook, 'SELECT cast(year_start_1 as varchar(8)) as year_start_1, DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2) + DATENAME(MONTH, substring(CAST(year_start_1 AS VARCHAR(8)),4,2) + DATENAME(YEAR, substring(CAST(year_start_1 AS VARCHAR(8)),1,4))) FROM web_statements')"

It's just throwing up an error and I not sure why:

Server was unable to process request

I have tried using convert but it doesn't work.

Any ideas?

UPDATE

with Chris's suggestion

Dim query as String = "Select * from openquery (devbook, 'SELECT year_start_1, cast(year_start_1 as varchar(8)) as year_start_1, substring(CAST(year_start_1 AS VARCHAR(8)),7,2)+''/''+substring(CAST(year_start_1 AS VARCHAR(8)),5,2)+''/''+substring(CAST(year_start_1 AS VARCHAR(8)),1,4) FROM web_statements')"

Still getting the error

Thanks

UPDATE

Couldn't seem to get it to work within the query so had to do a work around in the ASP.Net code

'POINTS END DATE YEAR
Dim strPointsDateEndYear = Mid(drv.Row("year_end_1"), 3, 2)
Dim strPointsDateEndMonth = Mid(drv.Row("year_end_1"), 5,2)
Dim strPointsDateEndDay = Right(drv.Row("year_end_1"), 2)

Dim strPointsDateEnd As String = strPointsDateEndDay + "/" + strPointsDateEndMonth + "/" + strPointsDateEndYear

Thanks for the help though

+2  A: 

Your first DATENAME doesn't seem to close all its brackets before the next DATENAME starts:

DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2) + DATENAME[...]

Should I assume be:

DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2)) + DATENAME[...]

Edit: Though having fixed that minor error (and converted it to debug) I'm getting errors about casting strings to dates. I'm not sure what the datename stuff is meant to do but how about this:

DECLARE @year_start_1 int
SET @year_start_1 = 20100827 

SELECT cast(@year_start_1 as varchar(8)) as year_start_1, 
substring(CAST(@year_start_1 AS VARCHAR(8)),7,2)+'/'+substring(CAST(@year_start_1 AS VARCHAR(8)),5,2)+'/'+substring(CAST(@year_start_1 AS VARCHAR(8)),1,4)

(converted to a non table based select for test/debug purposes)

So what you would want for your final line of sql would be (untested):

Select * from openquery (devbook, 'SELECT cast(year_start_1 as varchar(8)) as year_start_1, substring(CAST(year_start_1 AS VARCHAR(8)),7,2)+'/'+substring(CAST(year_start_1 AS VARCHAR(8)),5,2)+'/'+substring(CAST(year_start_1 AS VARCHAR(8)),1,4) FROM web_statements')

Second Edit for debug notes:

I thought it might also be worth suggesting how to debug this sort of problem. The error message suggested that the linked server you were sending the sub-statement to was unable to process the request. The first thing to try in this case would be to run the request directly on the server to see what happens. In this case in fact just parsing it on its own would have revealed the first errors I got.

Once you have your statement running form management studio or whatever directly on the server you can try converting it back into the "openquery" style statement and see if ti still works. Bascially break down your complicated scenario into lots of smaller bits to test each one individually.

Chris
Can't seem to get this to work see my update with your bits in my query
Jamie Taylor
You don't need to copy everything I did above. The declare and set and indeed te @year_start_1 variable were just because I didn't have a table with suitable data in so I wanted to jsut create a variable with a test value in.
Chris
I see I have again updated my question but still giving the error am i doing something wrong?
Jamie Taylor
Have you tried running just the inner command on the linked server (through management studio or similar) or is that not an option (eg no direct access)? Its possible there is still a syntax error in the SQL, double check it all and try a trial cutdown version (eg just "select '27/08/2010'" and then try to build up your query. I see you did some escaping already but it is possible that there is something else that needs examining. Just try playing around with the inner query til you get it to work and then see work out what change fixed it... Without a linked server its hard for me to debug...
Chris
A: 

You are using the datename function on strings, but it should be used on a datetime value. They are nested inside each other so you would end up with just the day, and besided it doesn't even do what you are looking for.

To get the format that you asked for, you can just use string operations to split it up in it's components and add slashes between them:

substring(CAST(year_start_1 AS VARCHAR(8)),7,2) + '/' +
substring(CAST(year_start_1 AS VARCHAR(8)),5,2) + '/' +
substring(CAST(year_start_1 AS VARCHAR(8)),1,4)
Guffa
Its worth noting as well that the indexes are also wrong on those first two parts - they should be starting at 7 and 5, not 6 and 4.
Chris
Yep, I noticed the same as Chris, the indices seems to be wrong for the pointers they are supposed to be.
Dick Lampard
@Chris, Dick: Yes, that's right.
Guffa
A: 

hi,

select  convert(varchar(10),convert(smalldatetime,'20100827'),103) will return 08/27/2010 

so, your solution is:

select  convert(varchar(10),convert(smalldatetime,convert(varchar,year_start_1)),103) will return 27/08/2010
lakhlaniprashant.blogspot.com
convert doesn't seem to work not sure why
Jamie Taylor