开发者

.NET C# mysql SUBSTRING gives me System.Byte[]?

开发者 https://www.devze.com 2023-01-02 19:51 出处:网络
This this code: SELECT SUBSTRING(posted,1,4) as year FROM styles reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection);

This this code:

SELECT SUBSTRING(posted,1,4) as year FROM styles
reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
reader.Read();
Response.Write(reader[0].ToString());

I only get the string "System.Byte[]" printed out. How come?

If I use the software Mysql Query Browser I get the actual string from my datab开发者_如何转开发ase.

I understand that "Byte[]" is an arraylist but how do I convert this to a pure string?

The "posted"-field in my database contains a date like "2010-04-04 13:23:00" and I want to get only the year by using SUBSTRING.


The correct query is

SELECT DISTINCT SUBSTRING(CONVERT(varchar, posted, 111),1,4) as year FROM styles

It equals to

SELECT STR(YEAR(posted)) as year FROM styles-- YEAR returns int statement

First argument is converted, than substring extracted. 111 - the convertion format: http://www.mssqltips.com/tip.asp?tip=1145

Also try

reader["year"].ToString();

as far as you use this alias.


You will need to use the .GetString

ie

reader[0].GetString(0);

Additionally you can use the MySQL YEAR function to extract the year from your date.

ie

SELECT YEAR(date_field) FROM table
0

精彩评论

暂无评论...
验证码 换一张
取 消