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

x
?
Solved

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

Posted on 2015-02-03
5
Medium Priority
?
1,086 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:Murray Brown
[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
5 Comments
 
LVL 11

Accepted Solution

by:
HuaMinChen earned 1000 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 15

Assisted Solution

by:Lokesh B R
Lokesh B R earned 1000 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 52

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 70

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:Murray Brown
ID: 40595279
Thanks
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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