Solved

SQL query help with error

Posted on 2014-02-21
6
171 Views
Last Modified: 2014-02-21
Ok I am investigating a query that someone developed for my company:

CAST(CAST(SUBSTRING(KeyField, 7, 10) AS BIGINT) AS varchar) AS OrderLineLink

The keyfield column has data such as....
0000230008    retult....    8
0000290004    result....   4

but now im getting data in the Keyfield such as...

000023AER8

This is obv causing a crash. If the conversion is not possible how can I get it to use null?
0
Comment
Question by:deanlee17
  • 3
  • 3
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39876329
try only

SUBSTRING(KeyField, 10, 10) AS OrderLineLink
0
 

Author Comment

by:deanlee17
ID: 39876338
Can you explain how its working?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39876348
SUBSTRING(KeyField, 10, 10)

this will give you last charater in the given string

your string have length 10 , this formula provide last charater of it
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:deanlee17
ID: 39876364
Actually, sorry to change the question slightly, but ive just noticed it needs to start far right and move left until it hits aa zero, so....

0000290004  = 4
0000290014  = 14
0000290144  = 144

Thank you.
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 39876432
try this

charIndex( '0',Reverse(KeyField)),Substring (KeyField ,Len(KeyField)- charIndex( '0',Reverse(KeyField))+2,Len(KeyField))
0
 

Author Comment

by:deanlee17
ID: 39876447
Thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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