none
Split column and add 0 RRS feed

  • Question

  • Hello SQL Expert, 

    How are you today?
    I need your lights because I can't find a solution to my problem.
    I have an MCU column (see table below) and would like to have what's in the CO column.
    I know it may seem easy to you, but I start in SQL and the exercises I do make me progress but I am trying to make a database for myself and I am confronted with that and I have not yet seen all the functions.
    thank you in advance

    

    Thank you 

    Saturday, January 25, 2020 11:54 AM

Answers

  • I would be curious to know what is the underlying business problem you are trying to solve.

    If I understand the outline correctly, this may be what you are looking for:

    SELECT CASE WHEN MCU LIKE '[0-9][0-9][0-9]%'
                THEN '00' + substring(MCO, 1, 3)
           END  AS CO
    FROM   dbo.tbl


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    • Marked as answer by Milozebre Saturday, January 25, 2020 1:07 PM
    Saturday, January 25, 2020 12:45 PM

All replies

  • If I understand correctly, you want to get the CO column value for a row with a given MCU value. You could use a single SELECT query for this task, such as:

    SELECT CO
    FROM dbo.YourTable
    WHERE MCU = '150641S';

    This will return '00150'.


    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    Saturday, January 25, 2020 12:24 PM
    Moderator
  • Hello Dan, 

    thank you for answer.

    I misspoke, I'm sorry.

    I have the MCU column and I would like to create the CO column
    I have already the MCU column in my table and I would like to create CO column in SQL

    Thank you and sorry for the inconvenience

    Saturday, January 25, 2020 12:38 PM
  • I would be curious to know what is the underlying business problem you are trying to solve.

    If I understand the outline correctly, this may be what you are looking for:

    SELECT CASE WHEN MCU LIKE '[0-9][0-9][0-9]%'
                THEN '00' + substring(MCO, 1, 3)
           END  AS CO
    FROM   dbo.tbl


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    • Marked as answer by Milozebre Saturday, January 25, 2020 1:07 PM
    Saturday, January 25, 2020 12:45 PM

  • it's not a business problem. 

    I have more than 1000 Dr who books with codebar. 

    i found in internet application like "calibre" to encode books but a lot of my books arent recognized.

    So i try to make a DB myself. 

    I thought the site was a dedicated forum for help.
    If I needed help with my work, I would ask for it but it would not be on site because I am not an informatican.

    Thanks for the help.


    Saturday, January 25, 2020 12:56 PM
  • This is a forum for help. The reason I asked for the underlying problem is that I often seen people ask about X when they should have asked about something earlier in the process - there could be a better solution that leads to something that does involve the problem you are asking for.

    A database for Dr Who certainly counts as a business problem. It does not have to be a commercial business.


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    Saturday, January 25, 2020 1:01 PM
  • its for myself. 

    Anyway, thank you for your solution. its working. 

    i can continue my personal work. 

    have a nice weekend. 

    Saturday, January 25, 2020 1:07 PM