Showing posts with label case. Show all posts
Showing posts with label case. Show all posts

Thursday, March 22, 2012

Comparing dates in Case statement

hi

I am having problem in converting dates either to varchar or integer

I need to compare (just month and date part , not the year)

If

mm/dd (of current date) i.e 07/12 > 06/30

i.e if today's month and date is greater than june 30th then perform task A

if today's month and date is less than july 1st perform task B

i.e

07/12 < 07/01

please help

Thanks

Code Snippet

select case when (month(getdate())* 100 + day(getdate())) > 0630

then ... -- task a

else ... --task b

end

from ....

|||

Thank you very much , that helped

But I have a new problem

I am trying to execute task A

as below

SELECT
case
when (month(getdate())* 100 + day(getdate())) > 0630
then
(select * from dbo.V_FUN_SCOPES_LEVELS0506)
end

It gives error

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

|||

You need to use IF syntax instead of CASE syntax; give a look to the related article in books online. That should look something like:

Code Snippet

if (month(getdate())* 100 + day(getdate())) > 0630
select * from dbo.V_FUN_SCOPES_LEVELS0506

Also, beware of using "SELECT *" syntax from within a stored procedure or function.

|||

yea, it won't work in that manner.

Can you elaborate a little more on what you're trying to do and how it is going to be used.

From this is looks like you'll need a stored procedure or maybe a function.

|||

I think in you're probably just looking at some standard IF...ELSE control of flow.


IF (SELECT (month(getdate())* 100 + day(getdate()))) > 0630

PRINT 'do this'

ELSE

PRINT 'do that'

|||

Hi

the IF syntax works independently but If i try to create a store proc as below it gives error

CREATE PROCEDURE [dbo].[P_Latest]

AS

IF (month(getdate())* 100 + day(getdate())) > 0630
BEGIN
select * from dbo.V_FUN_SCOPES_LEVELS0506

END

ELSE
IF (month(getdate())* 100 + day(getdate())) < 0701
BEGIN
select * from dbo.V_FUN_SCOPES_LEVELS0506
END

G0

I need to display select * from PROCEDURE [dbo].[P_Latest]

i.e Exec PROCEDURE [dbo].[P_Latest] from SQL reporting services

P.S. Here the view dbo.V_FUN_SCOPES_LEVELS0506 is as below

I need the subquery for Display purpose

(select
a.student_id,
a.at_sss_read_score as Score_06,
b.at_sss_read_score as Score_07,
a.at_test_month + '/' + '20' + a.fcat_test_year as Date_06,
b.at_test_month + '/' + '20' + b.fcat_test_year as Date_07,
a.at_test_month as Month_06,
b.at_test_month as Month_07,
a.at_test_year as Year_06,
b.at_test_year as Year_07,
a.at_sss_read_level as Level_06,
b.at_sss_read_level as Level_07

from
(
(select student_id, at_sss_read_score ,at_test_month,at_test_year,at_sss_read_level
from
DW_STUDENT.DBO.AT_TEST
where AT_TEST_YEAR = right (year (getdate())-1 , 2)) a
--AND STUDENT_ID IN ('0011307')) a

full join

(select student_id, at_sss_read_score ,at_test_month,at_test_year,at_sss_read_level
from
DW_STUDENT.DBO.AT_TEST
where AT_TEST_YEAR = right (year (getdate()),2 )) b
--AND STUDENT_ID IN ('0011307')) b

on a.student_id = b.student_id
)

where
a.at_test_year is not null
and b.at_test_year is not null

)


|||

This might work:

Code Snippet

CREATE PROCEDURE [dbo].[P_Latest]
AS

IF (month(getdate())* 100 + day(getdate())) > 0630
select * from dbo.V_FUN_SCOPES_LEVELS0506
ELSE
select * from dbo.V_FUN_SCOPES_LEVELS0506

G0

But I feel like I am missing something major. I see no point to the IF statement nor the ELSE Statement. Also, the SELECT * from inside a procedure is a bad idea because the MEANING of the SELECT * statement is determined at compile time and not at run time. You should explicitly list the columns that you return.

I feel like I am going wrong with this. Anyone? Help?

|||

In your Reporting Services dataset, just set the command type to StoredProcedure and the QueryString to [dbo].[P_Latest].

That is the equivalent of select * from ...

|||

I chose to believe that that was just test code

|||

Kent Waldrop Jl07 wrote:

This might work:

Code Snippet

CREATE PROCEDURE [dbo].[P_Latest]
AS

IF (month(getdate())* 100 + day(getdate())) > 0630
select * from dbo.V_FUN_SCOPES_LEVELS0506
ELSE
select * from dbo.V_FUN_SCOPES_LEVELS0506

G0

But I feel like I am missing something major. I see no point to the IF statement nor the ELSE Statement. Also, the SELECT * from inside a procedure is a bad idea because the MEANING of the SELECT * statement is determined at compile time and not at run time. You should explicitly list the columns that you return.

I feel like I am going wrong with this. Anyone? Help?

I've always heard this... "...SELECT * from inside a procedure is a bad idea.." without really understanding why. I apologize if i'm deviating from the point but...

when you say compile time and not run time, do you mean if a store procedure is compiled with select * then subsequently, the table changes (ie. column added) the sp will return everything without the extra column?

|||Yes, that is exactly what I mean; it is for that reason that I will sometimes say that use of "SELECT *" leaves "land mines" that blow up sometime later.

Tuesday, March 20, 2012

Comparing CASE statement products

The query I have written below works fine. However, I now want to uncomment the WHERE clause below to find entries where the PatientAge does not fall between the PAGBeginningAge and PAGEndingAge. However, when I uncomment the WHERE clause line I receive the following error message:

Msg 156, Level 15, State 1, Line 28

Incorrect syntax near the keyword 'FROM'.

SELECT ampfm.rpt_AdtVisit.PatientFullName, ampfm.rpt_AdtVisit.AdmitPriorityCode, ampfm.dct_AdmitPriorityType.AdmitPriorityTypeName,

ampfm.rpt_AdtVisit.AccountNumber, ampfm.rpt_PatientDemographics.PatientAge, ampfm.rpt_PatientDemographics.PatientAgeGroup,

ampfm.rpt_PatientDemographics.PatientSex,

CASE WHEN PatientAgeGroup = '0 - 14' THEN '0' ELSE

CASE WHEN PatientAgeGroup = '15 - 24' THEN '15' ELSE

CASE WHEN PatientAgeGroup = '25 - 34' THEN '25' ELSE

CASE WHEN PatientAgeGroup = '35 - 44' THEN '35' ELSE

CASE WHEN PatientAgeGroup = '45 - 54' THEN '45' ELSE

CASE WHEN PatientAgeGroup = '55 - 64' THEN '55' ELSE

CASE WHEN PatientAgeGroup = '65 - 74' THEN '65' ELSE

CASE WHEN PatientAgeGroup = '75 - 79' THEN '75' ELSE

CASE WHEN PatientAgeGroup = '80 - OVER' THEN '80'

END END END END END END END END END AS PAGBeginningAge,

CASE WHEN PatientAgeGroup = '0 - 14' THEN '14' ELSE

CASE WHEN PatientAgeGroup = '15 - 24' THEN '24' ELSE

CASE WHEN PatientAgeGroup = '25 - 34' THEN '34' ELSE

CASE WHEN PatientAgeGroup = '35 - 44' THEN '44' ELSE

CASE WHEN PatientAgeGroup = '45 - 54' THEN '54' ELSE

CASE WHEN PatientAgeGroup = '55 - 64' THEN '64' ELSE

CASE WHEN PatientAgeGroup = '65 - 74' THEN '74' ELSE

CASE WHEN PatientAgeGroup = '75 - 79' THEN '79' ELSE

CASE WHEN PatientAgeGroup = '80 - OVER' THEN '200'

END END END END END END END END END AS PAGEndingAge

--WHERE PatientAge NOT BETWEEN PAGBeginningAge AND PAGEndingAge

FROM ampfm.dct_AdmitPriorityType INNER JOIN

ampfm.rpt_AdtVisit ON ampfm.dct_AdmitPriorityType.AdmitPriorityTypeCode = ampfm.rpt_AdtVisit.AdmitPriorityCode INNER JOIN

ampfm.rpt_PatientDemographics ON ampfm.rpt_AdtVisit.RegNum = ampfm.rpt_PatientDemographics.RegNum

Two things:

First, your WHERE statment needs to come after the last ON condition.

second, you are not allowed to alias the "column/expression list" of the SELECT clause like this in MS SQL Server as you can do from DB2; you will need to include the entire case statement in your WHERE clause.

You might be able to create an inline TVF to make this complicated CASE statement a little easier to read.

Maybe something like:

Code Snippet

alter function dbo.ageRange
( @.arg_ageGroup varchar(12)
)
returns table
as
return
( select cast(parsename(replace(@.arg_ageGroup,' - ','.'),2)
as int)
as lowPart,
cast( case when parsename(replace(@.arg_ageGroup,' - ','.'),1)
= 'over'
then '200'
else parsename(replace(@.arg_ageGroup,' - ','.'),1)
end as int)
as highPart
)

go

declare @.ageGroup table ( ageGroup varchar(9))
insert into @.ageGroup
select '0 - 14' union all
select '15 - 24' union all
select '25 - 34' union all
select '35 - 44' union all
select '45 - 54' union all
select '55 - 64' union all
select '65 - 74' union all
select '75 - OVER'

select ageGroup,
lowPart,
highPart
from @.ageGroup
cross apply ageRange(ageGroup)

Another idea would be to put your ranges in a table that included (1) the range, (2) the low and (3) the high of the range. Then you could use a CROSS APPLY to fetch the desired numeric LOW and HIGH of the range.

|||

I can't believe I had the WHERE statement before the FROM statement; boy this has been a long day.

I placed the entire case statement in the WHERE clause and everything is working fine now. Thanks!

BTW, I am not familiar with inline TVF. What is it or where could I read up on it?

|||

( I edited my previous response to include it; please take a look. )

|||

Thanks! I may try something like that on my next project.

sqlsql

Compare value

Hi,

I just start ti study SQL, Pls advice the solution for the case below :

I have 2 table : order_detail and ship_detail

Order_id Ship_id
Designation Designation
qty qty


I'd like to write the SQL command that show me the status of each order and the item is complete or non complete shipped

Thanks & Best regards
CitronWhat field ties the order detail to the ship detail (is ship_id the same as order_id ) ? What does the designation field store - how do you know when an item has been shipped or not ?

Monday, March 19, 2012

Compare Strings

Hello,
I need to compare two strings that are in different fields,
my first step was capture one substring and capture the
result(in this case all the characters inside the string
are equal), but i cant use this because in some records
the substring is different because one of the strings have
more 4 characters.
I send you the two strings and hope that you can help me,
sorry but the two strings are very large
1st string:
(declare @.P1 bigint set @.P1=NULL exec Content_Save
@.ContentID = @.P1 output, @.ScheduleID = 663, @.ContractID = 367, @.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate
= 'May 31 2004 5:06:07:570PM', @.File = N'WAP UH
Politica_367_663_200405311706_d920a611-1552-
)
2nd string:
(declare @.P1 bigint set @.P1=0 exec Content_Save @.ContentID
= @.P1 output, @.ScheduleID = 663, @.ContractID = 367,
@.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate = 'May
31 2004 5:06:07:570PM', @.File = N'WAP UH
Politica_367_663_200405311706_d920a611-1552-40f
)
The two strings are always equal near the end of the
string
"....N'.. and the next 45 characters" but the not always
are equal at the begining.
Thanks a lot,
Best RegardsHave you checked out the PATINDEX statement ?
J
>--Original Message--
>Hello,
>I need to compare two strings that are in different
fields,
>my first step was capture one substring and capture the
>result(in this case all the characters inside the string
>are equal), but i cant use this because in some records
>the substring is different because one of the strings
have
>more 4 characters.
>I send you the two strings and hope that you can help me,
>sorry but the two strings are very large
>1st string:
>(declare @.P1 bigint set @.P1=NULL exec Content_Save
>@.ContentID = @.P1 output, @.ScheduleID = 663, @.ContractID =>367, @.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate
>= 'May 31 2004 5:06:07:570PM', @.File = N'WAP UH
>Politica_367_663_200405311706_d920a611-1552-
>)
>2nd string:
>(declare @.P1 bigint set @.P1=0 exec Content_Save
@.ContentID
>= @.P1 output, @.ScheduleID = 663, @.ContractID = 367,
>@.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate = 'May
>31 2004 5:06:07:570PM', @.File = N'WAP UH
>Politica_367_663_200405311706_d920a611-1552-40f
>)
>The two strings are always equal near the end of the
>string
>"....N'.. and the next 45 characters" but the not always
>are equal at the begining.
>Thanks a lot,
>Best Regards
>.
>|||Thanks Julie
>--Original Message--
>Have you checked out the PATINDEX statement ?
>J
>>--Original Message--
>>Hello,
>>I need to compare two strings that are in different
>fields,
>>my first step was capture one substring and capture the
>>result(in this case all the characters inside the string
>>are equal), but i cant use this because in some records
>>the substring is different because one of the strings
>have
>>more 4 characters.
>>I send you the two strings and hope that you can help me,
>>sorry but the two strings are very large
>>1st string:
>>(declare @.P1 bigint set @.P1=NULL exec Content_Save
>>@.ContentID = @.P1 output, @.ScheduleID = 663, @.ContractID
=>>367, @.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate
>>= 'May 31 2004 5:06:07:570PM', @.File = N'WAP UH
>>Politica_367_663_200405311706_d920a611-1552-
>>)
>>2nd string:
>>(declare @.P1 bigint set @.P1=0 exec Content_Save
>@.ContentID
>>= @.P1 output, @.ScheduleID = 663, @.ContractID = 367,
>>@.IniDate = 'May 31 2004 5:06:04:000PM', @.EndDate = 'May
>>31 2004 5:06:07:570PM', @.File = N'WAP UH
>>Politica_367_663_200405311706_d920a611-1552-40f
>>)
>>The two strings are always equal near the end of the
>>string
>>"....N'.. and the next 45 characters" but the not
always
>>are equal at the begining.
>>Thanks a lot,
>>Best Regards
>>.
>.
>

Compare SQL objects

Is there a stored procedure to allow me to compare two
SQL objects? In my case for example I want to compare
two stored procedures on two different databases?

If there is no SP that does comparison, would there be
any code in SQL DMO that does this?

How does someone learn SQL DMO? Does SQL Server
2000 have by default DMO learning material or I have to
search for books?

Thank youHi

You can try scripting them using DMO and then doing a file compare on the
two files.

Other options are Redgate compare:
http://www.red-gate.com/sql/summary.htm

OR QALite:
http://www.rac4sql.net/qalite_main.asp

John

"serge" <sergea@.nospam.ehmail.com> wrote in message
news:AtHje.74139$JU3.1442129@.wagner.videotron.net. ..
> Is there a stored procedure to allow me to compare two
> SQL objects? In my case for example I want to compare
> two stored procedures on two different databases?
> If there is no SP that does comparison, would there be
> any code in SQL DMO that does this?
> How does someone learn SQL DMO? Does SQL Server
> 2000 have by default DMO learning material or I have to
> search for books?
>
> Thank you|||"serge" <sergea@.nospam.ehmail.com> wrote in message
news:AtHje.74139$JU3.1442129@.wagner.videotron.net. ..
> Is there a stored procedure to allow me to compare two
> SQL objects? In my case for example I want to compare
> two stored procedures on two different databases?
> If there is no SP that does comparison, would there be
> any code in SQL DMO that does this?
> How does someone learn SQL DMO? Does SQL Server
> 2000 have by default DMO learning material or I have to
> search for books?
>
> Thank you

As John suggested, you can use the DMO Script method and compare the output.
This should be fine for procs, but it can be a problem for tables, because
you may have constraints which have been assigned a name by the system - the
constraint definition is identical, but it will show up as a difference in
your diff. You may or may not regard this as a problem, but tools such as
the Red Gate one allow you to configure your comparison to take this into
account, as well as whether or not to compare indexes, if a comparison
should be case-sensitive etc.

As for learning SQL-DMO - Books Online and practice. If you're already
familiar with COM programming, that would be useful, but it's far from
essential. Unfortunately the Books Online documentation doesn't provide many
examples - it tends to describe the steps to follow for a certain task, but
doesn't also give an actual code sample. Even if you want to use SQL-DMO
from a compiled language like VB or C#, I suggest you consider also using a
language such as Perl or Python - it's usually much faster for quick tests
and ad hoc experiments.

Simon|||There is another software tool that compares and synchronises databases
called DB Ghost (www.dbghost.com)
and it does data as well as schema. It is also the foundation for a
SQL Server change management process that works in harmony with any
source control/configuration management system to provide a completely
scalable solution for any size development team that has to work on the
same schema at the same time.

I highly recommend you check it out.

Thursday, February 16, 2012

commandtimeout on privat adapter

Hi,
The adapter in an dataset, via generted code in VS2005, is defined private.
How can I set the commantimeout in this case.
For example, this a snipped from generated code on a dataset:
Public Overloads Overridable Function Fill(ByVal dataTable As
dsDeltaV.INTERFACEDataTable) As Integer
Me.Adapter.SelectCommand = Me.CommandCollection(0)
'following line is added manualy, but ofcourse overwritten next time the
generation tool has run:
Me.Adapter.SelectCommand.CommandTimeout = CommandTimeOut
If (Me.ClearBeforeFill = true) Then
dataTable.Clear
End If
Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
Return returnValue
End Function
Hello Bart,
I would like to suggest you add a custom partial class into the project
which is in the same namespace and has same name of your adapter class.
In that partial class, you could add your custom method to refer the
SelectCommand and other protected variable.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
|||Hi ,
How is everything going? Please feel free to let me know if you need any
assistance.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.