skip to Main Content

I tried to use "||" for concatenation:

The query I used:

"SELECT id, name, age, age||id FROM myTable;"

This is the output:

Can anyone tell me why the output is not 201 (in first column) and 162(in second column)?? Also it gives similar outputs when I use two attributes that are of varchar datatype and the above two attributes are of int datatype.

3

Answers


  1. There is no such concatenation in mySQL. This is Oracle SQL dialect. You have to use the CONCAT function in mysql

    SELECT id, name, age, CONCAT(age,id) FROM myTable
    
    Login or Signup to reply.
  2. Can anyone tell me why the output is not 201

    its because, in mysql, you need to enable PIPES_AS_CONCAT. in order to work with ||

    If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the
    SQL-standard string concatenation operator (like CONCAT()).

    You can set it using phpmyadmin->variables->searchFor SQL_MODE

    enter image description here

    Refer mysql doc

    But i would suggest you to use

    CONCAT(columnName1, columnName2, ...)
    
    Login or Signup to reply.
  3. visit the Mysql Documentations :
    on this link you will find a list of String Functions and Operators

    but you not use|| to concatenate caratcters or strings but do this :

    1. SELECT id, name, age, concat(age,id) FROM myTable; or
    2. SELECT id, name, age, concat_ws(' ',age,id) FROM myTable; if you want to space the age with id like this for example 23 1. 23 for age and 1 for id.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search