Solved

SQL Argument data type decimal is invalid for argument 1 of substring function.

Posted on 2015-02-03
5
640 Views
Last Modified: 2015-02-06
Hi

I am trying to list all records where the number starts with a 2 but I get the error

Argument data type decimal is invalid for argument 1 of substring function.

I know that the following syntax doesn't work. What would the correct syntax be?

Select * From MARA_MBEW Where SubString([Total Stock],1, 1) = 2
0
Comment
Question by:murbro
5 Comments
 
LVL 10

Accepted Solution

by:
HuaMinChen earned 250 total points
ID: 40585716
Try
Select * From MARA_MBEW Where SubString(cast([Total Stock] as varchar),1, 1) = 2

Open in new window

0
 
LVL 14

Assisted Solution

by:Lokesh B R
Lokesh B R earned 250 total points
ID: 40585736
Hi,

Your column [Total Stock] is of type decimal.
SQL Function SUBSTRING(expression, start, length)  as Parameters where expression is of type VARCHAR
So you need to CAST/CONVERT [Total Stock] as VARCHAR.

Select * From MARA_MBEW Where SUBSTRING(CAST([Total Stock] AS VARCHAR),1, 1) = 2

Open in new window


You can try this to get all the records starts with 2
 Select * From MARA_MBEW Where [Total Stock] LIKE '2%'  

Open in new window

0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40586232
Use LEFT function:
Select * From MARA_MBEW Where LEFT([Total Stock],1) ='2'

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40586472
Lokesh is correct, the best style for performance is:

Where [Total Stock] Like '2%'
0
 

Author Closing Comment

by:murbro
ID: 40595279
Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how the fundamental information of how to create a table.

863 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

27 Experts available now in Live!

Get 1:1 Help Now