Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 LEFT() function

Author  Topic 

LordBucketHead
Starting Member

3 Posts

Posted - 2014-10-01 : 16:39:43
Hi all,

Novice. I'd like to return the left-most character from an 8 character string & the third from the left character too.

Like this ABC00123 returns AC

Could anyone suggest how I can modify my novice code to get there. Thank you very much for reading my post. Much obliged;

$query = "SELECT LEFT(uninum,3), RIGHT(uninum,5), clmarea, Date FROM tblunimov";
$result = mysql_query($query) or die(mysql_error());

echo "<div class='tblstyle1'>";
echo "<table class='tblstyle1'>";
echo "<tr><th>ini</th><th>item</th><th>area</th><th>date</th></tr>";
while($row = mysql_fetch_array($result)){

echo "<tr><td>";
echo $row['LEFT(uninum,3)'];
echo "</td><td>";
echo $row['RIGHT(uninum,5)'];
echo "</td><td>";
echo $row['clmarea'];
echo "</td><td>";
echo $row['Date'];
echo "</td></tr>";
}
echo "</table>";
echo "</div>";
?>

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-01 : 17:02:27
Your code appears to be using mysql. SQLTeam.com is for Microsoft SQL Server. I am not sure if this syntax will work or not on mysql, but here you go:

SELECT LEFT(uninum,1) + SUBSTRING(uninum,3,1), clmarea, Date FROM tblunimov

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

LordBucketHead
Starting Member

3 Posts

Posted - 2014-10-01 : 17:29:35
quote:
Originally posted by tkizer

Your code appears to be using mysql. SQLTeam.com is for Microsoft SQL Server. I am not sure if this syntax will work or not on mysql, but here you go:

SELECT LEFT(uninum,1) + SUBSTRING(uninum,3,1), clmarea, Date FROM tblunimov

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/



It didn't work but could it be because the column in question is datatype CHAR(3) whereby I'm looking to return only two characters ?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-01 : 17:39:53
What error did you get?

Did you want them in 2 separate columns? The code I posted is for it to return in one column, hence the usage of + where I am concatenating the two characters together.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

LordBucketHead
Starting Member

3 Posts

Posted - 2014-10-01 : 17:46:36
quote:
Originally posted by tkizer

What error did you get?

Did you want them in 2 separate columns? The code I posted is for it to return in one column, hence the usage of + where I am concatenating the two characters together.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/



I got no error. The + didn't concatenate. Merely a blank result. Two separate columns would suit my needs & that's how I'll deal with this problem. I thank you for your help. You certainly pointed me in the right direction. Much obliged to you.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-01 : 17:53:11


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -