Showing posts with label weird. Show all posts
Showing posts with label weird. Show all posts

Tuesday, March 27, 2012

cursor insert with like %

Hi I have a weird problem I want to cursor thru the values in a
temporary table and use the values to do a select statement to insert
into another temporary table...This select statement uses a like clause

something like where...when I take off the insert still nothing comes
back from the select...when I hardcode values it works...I get
results...is there something wrong with appending a +'%' to a value
read from a cursor?

DECLARE @.DEPT VARCHAR(65)
SET @.DEPT = "00201,00203"

DECLARE @.TB_ABSENCES TABLE(DeptOrEmpId VARCHAR(65))
DECLARE @.TB_DEPT TABLE ( V_DEPARTMENT_CODE VARCHAR(128) )

INSERT INTO @.TB_DEPT (V_DEPARTMENT_CODE)
SELECT V_DEPT FROM [ISIS].[dbo].[FU_GET_DEPTS_FROM_STRING](',', @.DEPT)

DECLARE DEPTS CURSOR FAST_FORWARD FOR
SELECT V_DEPARTMENT_CODE+'%' FROM @.TB_DEPT

OPEN DEPTS
FETCH NEXT FROM DEPTS INTO @.DEPT_CODE

WHILE @.@.FETCH_STATUS = 0
BEGIN

--INSERT INTO @.TB_ABSENCES TABLE
SELECT Code from TB_EMPLOYEE_DEPARTMENT T2
WHERE T2.V_HIERARCHY_CODE LIKE @.DEPT_CODE + '%'

FETCH NEXT FROM DEPTS INTO @.DEPT_CODE

END

CLOSE DEPTS
DEALLOCATE DEPTSyurps (yurps@.yahoo.co.uk) writes:
> Hi I have a weird problem I want to cursor thru the values in a
> temporary table and use the values to do a select statement to insert
> into another temporary table...This select statement uses a like clause
> something like where...when I take off the insert still nothing comes
> back from the select...when I hardcode values it works...I get
> results...is there something wrong with appending a +'%' to a value
> read from a cursor?

I would guess that there are trailing spaces. Rewrite as:

DECLARE @.DEPT VARCHAR(65)
SET @.DEPT = "00201,00203"

DECLARE @.TB_ABSENCES TABLE(DeptOrEmpId VARCHAR(65))

INSERT INTO @.TB_ABSENCES TABLE
SELECT Code
from TB_EMPLOYEE_DEPARTMENT T2
JOIN [ISIS].[dbo].[FU_GET_DEPTS_FROM_STRING](',', @.DEPT) D
ON T2.V_HIERARCHY_CODE LIKE rtrim(V_DEPT) + '%'

Yeah, that's right. No cursor. There is no need for it, and it could
be costly in terms of performance.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Oh gosh !! a couple hours wasted on something that I didn't need
to...

thanks!

Thursday, March 8, 2012

Culture overriding custom format

Hi, I've been having a weird problem with a report I'm making. I've done douzan of em, and the problem only appeared in one. Let say I have a text field, and I give it a format property (for the sake of the example, imagine its a date, and I set it as yyyy/MM/dd. Same result if I would use an expression with the Format function).

Now, depending on culture, the slashes gets overrided. Everything else work (so if I put yyyy/MM, the format is recognised and the day won't show...but it will use dashes instead of slashes, for example, with us culture).

How do I get it to always respect my format? I have, like I said, douzans of other reports and it works fine, the format string gets applied exactly: I don't know what the hell I could have changed. All the culture settings are at default, like in the reports that do work... I've tried diff-ing the source, can't see anything weird, but I'm sure I'm just being blind.

Any ideas?

Thank you.

The DateSeparator is defined on the DateTimeFomatInfo objects and the slash is being "translated" to the current culture.

If there's not an option to have the slash in the format string not interpreted as a DateSeparator, the only thing I can think of is to add a .Replace(CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator, "/") to the end of your Format statement.

|||

Just found this stumbling around on something else. Try "yyyy'/'mm'/'dd" as your format string. Found ithere, so you can fuss at Microsoft if it doesn't work.

|||

Makes sense. Yeah, escaping the slashes (let say, yyyy\/MM\/dd ) worked too.

But then, what do I do with numeric formats? (since the date was just an example). #,##0.00
Works fine in most of my reports, but in that particular one, no matter what format I put, the number gets translated with the current culture. What mostly gets me, is that it works in all the reports! Just not -that- one...ugh.|||

Those separators are also defined in a culture-specific way. I'd recheck that one report and make sure that you haven't accidentally specified a culture or that you did specify a culture on the other reports.

|||

Yup, you're right... I'm baffled, I tested the other reports, and not only did they have cultures set, but the formats never worked (I had a formula that switched the Language setting depending on a parameter set by the user, and the culture I always ended up printing my report coincidently had the correct separators).

My apologies for misleading you, I know it can be annoying when you're trying to help someone and they give you the wrong info :)

At least now I know I can backslash escape the magical separators... That is quite awkward that it does that, especially since its a bit different from the behavior of .NET formatters... Oh well!