homepage Welcome to WebmasterWorld Guest from 174.129.103.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
cast a substring in mysql, possible?
bigbear17




msg:4123458
 7:53 am on Apr 28, 2010 (gmt 0)

hello.
is there a way which i am able to cast/ convert my substrings into integer?
This is what i have been trying to do, apparently, it does not work.

SELECT campaignmonth

CAST(campaignmonth, substring(campaignmonth,1,1)as Jan)


FROM campaign a;

thanks

 

coopster




msg:4124314
 1:19 pm on Apr 29, 2010 (gmt 0)

Welcome to WebmasterWorld, bigbear17.

You have your syntax incorrect. CAST in MySQL requires UNSIGNED or SIGNED and the INTEGER part is optional in the syntax. If your campaignmonth column was of type DATE and formatted as yyyy-mm-dd, the query might look like this to get the month portion as an integer value ...
SELECT 
a.campaignmonth,
CAST(SUBSTRING(a.campaignmonth,6,2) AS UNSIGNED INTEGER) AS monthInteger
FROM campaign AS a
;

Resource:
Cast Functions and Operators [dev.mysql.com]

coopster




msg:4124319
 1:28 pm on Apr 29, 2010 (gmt 0)

Oh, one more thing I was going to mention. When you use the MySQL API (like with a programming language such as PHP), the values are often returned as string anyway. Yes, the value will indeed be an INTEGER value, but the actual type cast may most likely be STRING. Just a heads up.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved