Computed Column Equal to Letter

  • I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

  • usually when you say u need a computed column, u need to know how u want to have a resulting value For example if u have a table 'Order'. you have columns Qty and Rate and u want to have a computed column of TotalValue then the expression will be

    TotalValue = Qty * Rate

    Below the Schema.

    CREATE TABLE [dbo].[Order](

    [Item] [nchar](10) NULL,

    [Qty] [float] NULL,

    [Rate] [float] NULL,

    [TotalValue] AS ([Qty]*[Rate])

    ) ON [PRIMARY]

    you need to share your table schema, formula how to calculate the value 'Y'.

    if all you need a value 'Y', regardless any formula, you can a column 'Letter' with Chart(1) and set the Default value to 'Y'.

    and thats all.

    hope it helps

  • ALTER TABLE [tablename] ADD [columnname] AS CAST('Y' as char(1))

    Far away is close at hand in the images of elsewhere.
    Anon.

  • scotsditch (5/8/2014)


    I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

    I see that David provided the code to do this. However, what is the point??? If every row is always the same why do you need to store the value at all? Seems totally pointless to me.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (5/9/2014)


    scotsditch (5/8/2014)


    I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

    I see that David provided the code to do this. However, what is the point??? If every row is always the same why do you need to store the value at all? Seems totally pointless to me.

    +1

    I was waiting for the op to say they really wanted a new column with a default value !!!!

    Far away is close at hand in the images of elsewhere.
    Anon.

  • David Burrows (5/9/2014)


    Sean Lange (5/9/2014)


    scotsditch (5/8/2014)


    I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

    I see that David provided the code to do this. However, what is the point??? If every row is always the same why do you need to store the value at all? Seems totally pointless to me.

    +1

    I was waiting for the op to say they really wanted a new column with a default value !!!!

    +2

    A view would do the same as the "computed column". A column with a default value would allow you to store and update in the future should the requirement come.

    It still begs the question "Why?????".

  • It still begs the question "Why?????".

    BWAHAA!!!! Was that pun intended??? 😀

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (5/9/2014)


    It still begs the question "Why?????".

    BWAHAA!!!! Was that pun intended??? 😀

    🙂

  • Sean Lange (5/9/2014)


    scotsditch (5/8/2014)


    I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

    I see that David provided the code to do this. However, what is the point??? If every row is always the same why do you need to store the value at all? Seems totally pointless to me.

    The value's not being stored, since it's a computed column (and not involved in an index or something else that requires persisting it).

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • ScottPletcher (5/9/2014)


    Sean Lange (5/9/2014)


    scotsditch (5/8/2014)


    I'm trying to do something so simple, it's driving me nuts. Especially since I remember doing it before.

    I want to create a computed Column in a SQL Server table, and I want every entry in that column to be the letter Y.

    Can someone please tell me what formula I need to type in the formula box in the properties pane of the computed column to pull that off?

    Thanks,

    I see that David provided the code to do this. However, what is the point??? If every row is always the same why do you need to store the value at all? Seems totally pointless to me.

    The value's not being stored, since it's a computed column (and not involved in an index or something else that requires persisting it).

    I still don't see the point if the value is always the same thing? There is no actual benefit that I can come up with to bother with such a thing. It is a constant.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply