Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access SQL Statement to SQL Server

Posted on 2006-07-10
5
Medium Priority
?
260 Views
Last Modified: 2008-03-10
Is there a way to recreate this Access SQL Command in SQL Server?

Switch([DWELL_DT]=tblACTIVE.BASD And DateDiff("d",tblACTIVE.BASD,Now())/30<24 Or [Dwell_DT]=[PEBD] And DateDiff("d",tblACTIVE.PEBD,Now())/30<24 Or [DWELL_DT]=[BESD] And DateDiff("d",tblACTIVE.BESD,Now())/30<24,'B',[DWELL_DT]=tblACTIVE.BASD And DateDiff("d",tblACTIVE.BASD,Now())/30>24 Or [DWELL_DT]=tblACTIVE.PEBD And DateDiff("d",tblACTIVE.PEBD,Now())/30>24 Or [DWELL_DT]=tblACTIVE.BESD And DateDiff("d",tblACTIVE.BESD,Now())/30>24,'N',[DWELL_DT]<>tblACTIVE.BASD And DateDiff("d",[DWELL_DT],Now())/30>0 And DateDiff("d",[DWELL_DT],Now())/30<=12,'1',[DWELL_DT]<>tblACTIVE.BASD And DateDiff("d",[DWELL_DT],Now())/30>12 And DateDiff("d",[DWELL_DT],Now())/30<=24,'2',[DWELL_DT]<>tblACTIVE.BASD And DateDiff("d",[DWELL_DT],Now())/30>24,'3',IsNull([DWELL_DT]),'D') AS Dwell
0
Comment
Question by:cory_booth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17074130
DATEDIFF(dd, urColumn, GETDATE())
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17074149
something like this:

select case when [DWELL_DT]=tblACTIVE.BASD And DateDiff(day,tblACTIVE.BASD,Getdate())/30<24 Or [Dwell_DT]=[PEBD] And DateDiff(day,tblACTIVE.PEBD,getdate())/30<24 Or [DWELL_DT]=[BESD] And DateDiff(day,tblACTIVE.BESD,getdate())/30<24 then 'B'[DWELL_DT]=tblACTIVE.BASD And DateDiff("d",tblACTIVE.BASD,Now())/30>24 Or [DWELL_DT]=tblACTIVE.PEBD And DateDiff(day,tblACTIVE.PEBD,getdate())/30>24 Or [DWELL_DT]=tblACTIVE.BESD And DateDiff(day,tblACTIVE.BESD,getdate())/30>24,'N',[DWELL_DT]<>tblACTIVE.BASD And DateDiff(day,[DWELL_DT],getdate())/30>0 And DateDiff(day,[DWELL_DT],getdate())/30<=12 then '1' when [DWELL_DT]<>tblACTIVE.BASD And DateDiff(day,[DWELL_DT],getdate())/30>12 And DateDiff(day,[DWELL_DT],getdate())/30<=24 then  '2' when [DWELL_DT]<>tblACTIVE.BASD And DateDiff(day,[DWELL_DT],getdate())/30>24 then '3' else IsNull([DWELL_DT]),'D') end AS Dwell

however, you will need more () around the and and or clauses otherwise you get wrong results.
0
 

Author Comment

by:cory_booth
ID: 17074236
Switch(
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, dbo.EMILPO_SOLD_T.BASD, GETDATE()) / 30 < 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = [dbo.EMILPO_SOLD_T.PEBD] AND DateDiff(dd, dbo.EMILPO_SOLDER_T.PEBD, GETDATE()) / 30 < 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = [dbo.EMILPO_SOLD_T.BESD] AND DateDiff(dd, dbo.EMILPO_SOLDER_T.BESD, GETDATE()) / 30 < 24, 'B',
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, dbo.EMILPO_SOLD_T.BASD, GETDATE()) / 30 > 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLDER_T.PEBD AND DateDiff(dd, dbo.EMILPO_SOLDER_T.PEBD, GETDATE()) / 30 > 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLDER_T.BESD AND DateDiff(dd, dbo.EMILPO_SOLDER_T.BESD, GETDATE()) / 30 > 24, 'N',
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE())
                      / 30 > 0 AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE()) / 30 <= 12, '1',
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE())
                      / 30 > 12 AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE()) / 30 <= 24, '2',
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE()) / 30 > 24,
                      '3', IsNull([dbo.EMILPO_SOLD_T.DWELL_ST_DT]), 'D') AS Dwell

I receive errors on the Parse statement
0
 

Author Comment

by:cory_booth
ID: 17074322
CASE WHEN (([dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, dbo.EMILPO_SOLD_T.BASD, GETDATE())
                      / 30 < 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = [dbo.EMILPO_SOLD_T.PEBD] AND DateDiff(dd, dbo.EMILPO_SOLD_T.PEBD, GETDATE()) / 30 < 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = [dbo.EMILPO_SOLD_T.BESD] AND DateDiff(dd, dbo.EMILPO_SOLD_T.BESD, GETDATE()) / 30 < 24) THEN 'B',
                      ([dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, dbo.EMILPO_SOLD_T.BASD, GETDATE()) / 30 > 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.PEBD AND DateDiff(dd, dbo.EMILPO_SOLD_T.PEBD, GETDATE()) / 30 > 24 OR
                      [dbo.EMILPO_SOLD_T.DWELL_ST_DT] = dbo.EMILPO_SOLD_T.BESD AND DateDiff(dd, dbo.EMILPO_SOLD_T.BESD, GETDATE()) / 30 > 24) THEN 'N',
                      ([dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE())
                      / 30 > 0 AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE()) / 30 <= 12) THEN '1',
                      ([dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE())
                      / 30 > 12 AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE()) / 30 <= 24) THEN '2',
                      ([dbo.EMILPO_SOLD_T.DWELL_ST_DT] <> dbo.EMILPO_SOLD_T.BASD AND DateDiff(dd, [dbo.EMILPO_SOLD_T.DWELL_ST_DT], GETDATE())
                      / 30 > 24) THEN '3', (IsNull([dbo.EMILPO_SOLD_T.DWELL_ST_DT]) THEN 'D')) END AS Dwell


Incorrect keyword near "THEN"

0
 

Author Comment

by:cory_booth
ID: 17074592
Looked up the case statement,

Had to throw in the WHENS
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question