Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add text to my column's select statement

Tags:

sql-server

I want to prepend text to sql select,

I can do this:

SELECT (1328724983-time)/60/60 AS status 
FROM voting 
WHERE account = 'ThElitEyeS' AND vid = 1 

This will show time left.

What I'd like to do is this though:

SELECT 'Please try after' + (1328724983-time)/60/60 AS status 
FROM voting 
WHERE account = 'ThElitEyeS' AND vid = 1 

Is there any way I can accomplish this?

like image 810
ThElitEyeS Avatar asked Feb 08 '12 19:02

ThElitEyeS


People also ask

How do you add a text field in SQL?

To add a new column to a table in MySQL, you use the ALTER TABLE statement in a similar way to other databases. For example, to add a text value to a customer table: ALTER TABLE customer ADD COLUMN suburb VARCHAR(100); The keyword COLUMN is actually optional.

How do you display text in SQL query?

:Explanation: Note: You can use literal string (enclosed in single or double quotation mark) just like we use a column name in the SELECT statement. If you use the literal string with a column then it will be displayed in every row of the query results.


1 Answers

Just use cast or convert to convert it all to varchar for instance.

SELECT 'Please try after' + CAST((1328724983-time)/60/60 as varchar(80)) AS status 
  FROM voting 
  WHERE account = 'ThElitEyeS' AND vid = 1;

See the MSDN on Cast / Convert

Based on your comments you can do:

SELECT 'Please try again after' + CAST(MyColForHours as varchar(25)) + ' hours', AnyOtherColumns FROM Table

like image 153
JonH Avatar answered Nov 13 '22 09:11

JonH