Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Tuesday, March 27, 2012

enumerate suffixes and counts in varchar column

I have a column of VARCHAR values for which each value may contain one of
more dashes ("-" character). I would like T-SQL that enumerate all the
possible values following the FINAL dash character and counts the number of
occurrances of each and loads these into another table (2 col table :
Suffix[nvarchar] and Count[int])First, the obligitory note of reason. Storing data like this is generally a
bad idea. If you need to use substring on a value then it probably needs to
be > 1 columns. Maybe not in your case, but I figure that the dashes aren't
a random pattern, right?
Second, this will give you the result, I think...
create table test
(
value varchar(20)
)
insert into test
select 'hds-f-dsafsaf-asf'
union all
select 'asdfasdfads'
union all
select 'asdfads-asdfas-as'
union all
select 'isd-asd'
go
select case when charindex('-',value) > 0 then
substring(reverse(value),1,charindex('-',reverse(value))-1)
else ''
end
from test
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:Oxjgp2QLGHA.904@.TK2MSFTNGP10.phx.gbl...
>I have a column of VARCHAR values for which each value may contain one of
>more dashes ("-" character). I would like T-SQL that enumerate all the
>possible values following the FINAL dash character and counts the number of
>occurrances of each and loads these into another table (2 col table :
>Suffix[nvarchar] and Count[int])
>|||Yes, I agree. Very bad data structure. Not my idea.
You're code looks interesting, but I also need a count of each distinct
suffix (where a distinct suffix is defined as one that follows the final
dash ("-") character.
"Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
news:%23hLtR%23QLGHA.2416@.TK2MSFTNGP15.phx.gbl...
> First, the obligitory note of reason. Storing data like this is generally
> a bad idea. If you need to use substring on a value then it probably
> needs to be > 1 columns. Maybe not in your case, but I figure that the
> dashes aren't a random pattern, right?
> Second, this will give you the result, I think...
> create table test
> (
> value varchar(20)
> )
> insert into test
> select 'hds-f-dsafsaf-asf'
> union all
> select 'asdfasdfads'
> union all
> select 'asdfads-asdfas-as'
> union all
> select 'isd-asd'
> go
> select case when charindex('-',value) > 0 then
> substring(reverse(value),1,charindex('-',reverse(value))-1)
> else ''
> end
> from test
>
> --
> ----
--
> Louis Davidson - http://spaces.msn.com/members/drsql/
> SQL Server MVP
> "Arguments are to be avoided: they are always vulgar and often
> convincing."
> (Oscar Wilde)
> "John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> news:Oxjgp2QLGHA.904@.TK2MSFTNGP10.phx.gbl...
>|||Here is the complete solution. Hope this helps get you going...
create table test
(
value varchar(20)
)
insert into test
select 'hds-f-dsafsaf-asf'
union all
select 'asdfasdfads'
union all
select 'asdfads-asdfas-as'
union all
select 'isd-asd'
union all
select 'isd3-asdfa-asd'
go
select suffix, count(*)
from ( select reverse(case when charindex('-',value) > 0 then
substring(reverse(value),1,charindex('-',reverse(value))-1)
else ''
end) as suffix
from test ) as suffixValues
group by suffix
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uhJPFDRLGHA.2668@.tk2msftngp13.phx.gbl...
> Yes, I agree. Very bad data structure. Not my idea.
> You're code looks interesting, but I also need a count of each distinct
> suffix (where a distinct suffix is defined as one that follows the final
> dash ("-") character.
>
> "Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
> news:%23hLtR%23QLGHA.2416@.TK2MSFTNGP15.phx.gbl...
>

enum for a column

Is there a way to do something like an enum for a column?
I have a column that should contain one of a fixed set of values. The set of
values won't change and there is only one column in one table that uses this
set of values.
So I was thinking that there wasn't much point in creating a new table just
for this set of values. I could set the data type of the column to a fixed
size char and create a check constraint that tests for each possible value.
There are 20 values in the set so the constraint will be tedious to write.
But there is a notion of order to the set. i.e. value 1 < value 2 < value
N.
So what I really want is an enum like in C++ or C#.
What's the closest approximate in T-SQL?the closest thing to an enum in TSQL is a check constraint.
if you use IN the definition isn't that much more tedious than an enum
definition in C#--you still have to list all of the possible values. For
example,
create table t1
(
f1 int identity(1,1) not null
constraint pk_t1 primary key clustered,
f2 char(5) not null
constraint ck_f2 check (f2 in ('NORTH', 'SOUTH', 'EAST', 'WEST'))
)
"Jonathan Dodds" wrote:

> Is there a way to do something like an enum for a column?
> I have a column that should contain one of a fixed set of values. The set
of
> values won't change and there is only one column in one table that uses th
is
> set of values.
> So I was thinking that there wasn't much point in creating a new table jus
t
> for this set of values. I could set the data type of the column to a fixed
> size char and create a check constraint that tests for each possible value
.
> There are 20 values in the set so the constraint will be tedious to write.
> But there is a notion of order to the set. i.e. value 1 < value 2 < value
> N.
> So what I really want is an enum like in C++ or C#.
> What's the closest approximate in T-SQL?
>
>

Sunday, February 26, 2012

Enterprise Manager Database Diagram Wizard

I am creating a Table Diagram via Enterprise Manager and the Database Diagra
m
Wizard.
When I copy and paste into Excel, it creates a first column with the numbers
0,1, and 2. Each of these numbers is next to a column. Are these numbers
indicating keys or indexes or what?
Let me know.
Thanks!Hi
Testing this out it seems that this is some internal code, the first column
always seems to have a 1 all other columns seem to be 0, unless it is the PK
which seems to add two to the value i.e. 3 if it is the first column and 2
if not.
Nullability/Datatype/FKs/Indexes do not seem to effect the value.
John
"wnfisba" <wnfisba@.discussions.microsoft.com> wrote in message
news:E7F36117-2633-4F45-994F-3234A9843978@.microsoft.com...
>I am creating a Table Diagram via Enterprise Manager and the Database
>Diagram
> Wizard.
> When I copy and paste into Excel, it creates a first column with the
> numbers
> 0,1, and 2. Each of these numbers is next to a column. Are these numbers
> indicating keys or indexes or what?
> Let me know.
> Thanks!

Enterprise Manager Create Date

Folks,
When viewing the tables of the databases we have on our Server, the
values in the "Create Date" column are generally of the format
"DD/MM/YYYY HH:MM:SS". Curiously however, I just had to recreate one
of the tables, and on that table alone the create date displayed is
"YYYY-MM-DD HH:MM:SS.FFF" (FFF = fractions of a second I assume?)
Anyone know why this has changed? Could it be some kind of change of
regional settings on the Server?
Suggestions invited
Thanks
SteveSteve Hall wrote:
> Folks,
> When viewing the tables of the databases we have on our Server, the
> values in the "Create Date" column are generally of the format
> "DD/MM/YYYY HH:MM:SS". Curiously however, I just had to recreate one
> of the tables, and on that table alone the create date displayed is
> "YYYY-MM-DD HH:MM:SS.FFF" (FFF = fractions of a second I assume?)
> Anyone know why this has changed? Could it be some kind of change of
> regional settings on the Server?
See if it helps you:
Solving the Datetime Mystery (SQL Server Magazine)
http://www.winnetmag.com/SQLServer/.../9147/9147.html
sincerely,
--
Sebastian K. Zaklada
Skilled Software
http://www.skilledsoftware.com
This posting is provided "AS IS" with no warranties, and confers no rights.|||"Sebastian K. Zaklada" <szaklada-dont-like-spam@.skilledsoftware.com> wrote in message news:
<OoKQfbfBEHA.2804@.tk2msftngp13.phx.gbl>...
> Steve Hall wrote:
> See if it helps you:
> Solving the Datetime Mystery (SQL Server Magazine)
> http://www.winnetmag.com/SQLServer/.../9147/9147.html
> sincerely,
Sebastian,
Thanks for that... but I think I may be missing something still - that
information suggests how I can format dates returned from queries...
But this is the table's create date property - internal to SQL - where
is that format defined?
Forgive me if I am still missing your point!
Steve

Friday, February 17, 2012

Enterprise Manager - create table/column

I'm sorry, I've been searching for information that I need but I was in the
middle of the sea. Please point me to the right direction of where should I
post my question. Sorry.....
We are using SQL Server 2000. At design table view at SQL Server Enterprise
Manager, it has "Description", "Identity", "Identity Seed", "Formula" etc
fields at the bottom of the screen, and I would like to know what those are
for and their funtionalities.
Vanessa
Vanessa,
You might find it helpful to read the CREATE TABLE and CREATE VIEW articles
in the BOL, since the GUI is just creating these statements. Also, look up
"identity columns", "computed columns", and "extended properties".
RLF
"Vanessa" <Vanessa@.discussions.microsoft.com> wrote in message
news:073D607F-3087-47E4-ACE8-1BA0EA4ADD3B@.microsoft.com...
> I'm sorry, I've been searching for information that I need but I was in
> the
> middle of the sea. Please point me to the right direction of where should
> I
> post my question. Sorry.....
> We are using SQL Server 2000. At design table view at SQL Server
> Enterprise
> Manager, it has "Description", "Identity", "Identity Seed", "Formula" etc
> fields at the bottom of the screen, and I would like to know what those
> are
> for and their funtionalities.
> Vanessa
>

Wednesday, February 15, 2012

Entering Unique data into a column

Is there a better to insure an INSERT will only add a new row only if the
data being added to a particular column is unique, besides doing a SELECT
before doing the INSERT?
SELECT X from table where X='NewData'
ExecuteReader and test for number of Rows
if Zero then do the INSERT
Thank you...
BruceINSERT INTO YourTable (x, ...)
SELECT 'NewData', ...
WHERE NOT EXISTS
(SELECT *
FROM YourTable
WHERE x = 'NewData')
David Portas
SQL Server MVP
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:ecidneFpqq8UaW7cRVn-pA@.giganews.com...
> INSERT INTO YourTable (x, ...)
> SELECT 'NewData', ...
> WHERE NOT EXISTS
> (SELECT *
> FROM YourTable
> WHERE x = 'NewData')
> --
> David Portas
> SQL Server MVP
> --
>
Thanks.....
Bruce|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:ecidneFpqq8UaW7cRVn-pA@.giganews.com...
> INSERT INTO YourTable (x, ...)
> SELECT 'NewData', ...
> WHERE NOT EXISTS
> (SELECT *
> FROM YourTable
> WHERE x = 'NewData')
> --
> David Portas
> SQL Server MVP
> --
>
Okay, What am I doing wrong? The following didn't work.. The INSERT happened
even though the value of 'Dentist' was in the table in the columne
AddressType.
INSERT INTO AddressType(AddressType) Values ('Dentist')
SELECT 'Dentist'
WHERE NOT EXISTS
(SELECT *
FROM AddressType
WHERE AddressType = 'Dentist')
Now, my table, AddressType, had only two columns, PrimaryKey (with
autoincrement) and AddressType.
Thanks...
Bruce|||You added the VALUES clause, which means it's no longer an INSERT... SELECT.
Try:
INSERT INTO AddressType (AddressType)
SELECT 'Dentist'
WHERE NOT EXISTS
(SELECT *
FROM AddressType
WHERE AddressType = 'Dentist')
BTW I assume AddressType is declared unique. Always declare unique columns,
don't just rely on the INSERT.
David Portas
SQL Server MVP
--|||
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:L8udnQJ32Z_Hm2ncRVn-rA@.giganews.com...
> You added the VALUES clause, which means it's no longer an INSERT...
SELECT.
> Try:
> INSERT INTO AddressType (AddressType)
> SELECT 'Dentist'
> WHERE NOT EXISTS
> (SELECT *
> FROM AddressType
> WHERE AddressType = 'Dentist')
> BTW I assume AddressType is declared unique. Always declare unique
columns,
> don't just rely on the INSERT.
> --
> David Portas
> SQL Server MVP
> --
>
This works. Thanks. And no, the column is not declared unique. I forgot
about that attribute, but will add it...
Thanks again...
Bruce

Entering a row from a table to a column of other table

Is it possible to enter a complete record into a column say varchar(max) and then be able to retreive the record from the column. I am using sql 2005.

Thanks very much in advance,

you can use a separator for columns and insert it into a table.

insert into tablename (varcharnmaxcolumn)

select col1 +'|' + col2+'|' +col3

from table1

and during retrieval you can split the output as per separator as '|' in this case

Entering <NULL> into column via EM

How do I enter <NULL> into table column via EM?
I thought it was Shift + Enter or CTRL+Shift + Enter - but no dice.
Thanks.Its CTRL+0
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Guadala Harry" <GMan@.NoSpam.net> schrieb im Newsbeitrag
news:O9rXxMdSFHA.248@.TK2MSFTNGP15.phx.gbl...
> How do I enter <NULL> into table column via EM?
> I thought it was Shift + Enter or CTRL+Shift + Enter - but no dice.
> Thanks.
>|||CTRL + 0

Enter Today's Date in SSMS Open Table Grid View

What date constants are available for entering today's date in a
datetime column after using the Open Table command within SSMS? I
have tried GETDATE() and CURRENT_TIMESTAMP, but neither worked.
Another question - if I open the samed named table in two databases to
compare the data contents using Open Table, the tab labels only
display the table name and are not fully qualified. Is there an SSMS
option to show the table labels fully qualified.
Thanks in advance - Peter
pwc (pcrickman@.verizon.net) writes:
> What date constants are available for entering today's date in a
> datetime column after using the Open Table command within SSMS? I
> have tried GETDATE() and CURRENT_TIMESTAMP, but neither worked.
You look at your watch, and the type what you see. Open Table is a very
simple data-entry tool.
If you don't want to consult your watch, you will have to write an INSERT
or an UPDATE statement instead.

> Another question - if I open the samed named table in two databases to
> compare the data contents using Open Table, the tab labels only
> display the table name and are not fully qualified. Is there an SSMS
> option to show the table labels fully qualified.
Yes, it's called SP2. That is, they've changed this in SP2 so that the
database name is included.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx