Solved

Query Syntax in SSMS

Posted on 2016-07-28
5
28 Views
Last Modified: 2016-07-29
I have a query I am trying to write and I have a couple of issues retrieving data:

Issue #1: I am trying to return the right 2 characters in a field that appears to be left justified. The field is a combined 'CITY ST' with a space delimiter before the 2 character state. I obviously cannot determine precisely where in that 50 character, left justified field the actual 2 characters will be.

Issue #2: I am trying to write an expression that I did have working in an Access database (I am trying to move my queries to management studio). The query in Access was:

Round([PER_DIEM_INT]+(Round((GETDATE()-[PER_DIEM_DATE])*[PER_DIEM_AMT],2)),2)-[INT_COLL] AS INT_DUE

error: Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
0
Comment
Question by:sparker1970
  • 2
  • 2
5 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 500 total points
Comment Utility
Give us some sample data to work with, but as a guess...

#1 - You'll need to trim the trailing spaces before you can get the last two characters, something like..
Declare @citystate char(50) = 'EDEN PRAIRIE, MN                  '
SELECT RIGHT(RTRIM(@citystate), 2) 

Open in new window


#2
>error: Implicit conversion from data type datetime to float is not allowed.
Explain for us the GETDATE()-[PER_DIEM_DATE] part, are you trying to get the difference in days?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
try this for #2
ROUND( [PER_DIEM_INT] + ROUND( DATEDIFF(day,PER_DIEM_DATE, GETDATE() ) *[PER_DIEM_AMT] ,2) )

Think you need to use DATEDIFF() this returns an integer number of the units (I chose day as the unit)

Issue #1. Are you saying the state isn't always at the last 2 chars at the right?
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
Wild guess on #2..
SELECT ROUND(
  PER_DIEM_INT + 
  (DATEDIFF(days, GETDATE(), PER_DIEM_DATE) * PER_DIEM_AMT) -
  INT_COLL, 2) AS INT_DUE

Open in new window


btw check out Migrating your Access Queries to SQL Server Transact-SQL for a wompload of helpful Access SQL to SQL Server T-SQL tips.
0
 

Author Comment

by:sparker1970
Comment Utility
Paul...with the 2 "ROUND" statements there was a missing argument for one of them.

St. Jimbo...the RTRIM statement worked perfectly...your "wild guess" was spot on with one exception, it was day not days.
0
 

Author Closing Comment

by:sparker1970
Comment Utility
See my last post for response on solutions.

Thank you very much!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now