Showing posts with label hii. Show all posts
Showing posts with label hii. Show all posts

Wednesday, March 21, 2012

How to eliminate the commas from the end in sql query when the column value is null

HI

I have three different columns as email1,email2 , email3.I am concatinating these columns into one i.e EMail like

select ISNULL(dbo.tblperson.Email1, N'') +';'+ISNULL(dbo.tblperson.Email2, N'') +';'+ISNULL(dbo.tblperson.Email3, N'')ASEmail from tablename.

One eg of the output of the above query when email2,email3 are having null values in the table is :

jacky_foo@.mfa.gov.sg;;

means it is inserting semicoluns whenever there is a null value in the particular column. I want to remove this extra semicolumn whenever there is null value in the column.

Please let me know how can i do this

If you just change SQL a bit you have the answer, see below

select ISNULL(dbo.tblperson.Email1+ ';', N'') + ISNULL(dbo.tblperson.Email2 + ';', N'') + ISNULL(dbo.tblperson.Email3 + ';', N'')ASEmail from tablename.

|||

I tried this it worked a bit but not completely.Now I am getting the semicolumn at the end if there is null for the third column or you can say for the last column.

|||

There is probably a quick easy way to do it, but this will work:

select ISNULL(dbo.tblperson.Email1, N'') + ISNULL(CASE WHEN dbo.tblperson.Email1 IS NOT NULL THEN ';' ELSE '' END+dbo.tblperson.Email2, N'') + ISNULL(CASE WHEN dbo.tblperson.Email1 IS NOT NULL OR dbo.tblperson.Email2 IS NOT NULL THEN ';' ELSE '' END+dbo.tblperson.Email3, N'')ASEmail from tablename.

|||

Another way is to first normalize your data:

SELECT Email1 As Email FROM dbo.tblPerson
UNION ALL
SELECT Email2 As Email FROM dbo.tblPerson
UNION ALL
SELECT Email3 As Email FROM dbo.tblPerson

Now use the normalized data in a query like:

DECLARE @.Email varchar(max)

SELECT @.Email=ISNULL(@.Email+';','')+Email

FROM (

SELECT Email1 As Email FROM dbo.tblPerson
UNION ALL
SELECT Email2 As Email FROM dbo.tblPerson
UNION ALL
SELECT Email3 As Email FROM dbo.tblPerson

) t1

SELECT @.Email AS Email

|||

Another way is to use one of the many string concatenation techniques once your data has been normalized.

There is a CONCATENATE aggregation function that you can install that will do the trick. (Microsoft supplies one somewhere, google "T-SQL string concatenation aggregate").

There is another technique using the FOR XML/PATH to do the same thing, but it's also kind of messy.

|||

I appreciate your reply. This query worked for me.

Monday, March 12, 2012

How to duplicate a database?

Hi:
I have a database for my asp.net application and now I want to create
another database with everything same, including the data, except the
database name(this database will be used by another asp.net application).
How can I do this programmingly? I am using C#.
Thank you very much!
Quentin H.You can restore from a recent backup and specify the new name. Also, you can
detach, copy the mdf file, and then re-attach under a new name. How to do
this from C# ? You could use data management objects (DMO), but I would
suggest just writing a stored procedure and calling the SP from C#.
"Quentin Huo" <q.huo@.manyworlds.com> wrote in message
news:%23QcXgaiZFHA.3328@.TK2MSFTNGP09.phx.gbl...
> Hi:
> I have a database for my asp.net application and now I want to create
> another database with everything same, including the data, except the
> database name(this database will be used by another asp.net application).
> How can I do this programmingly? I am using C#.
> Thank you very much!
> Quentin H.
>|||Thank you for your quick reply!
Also, I want to ask how I can do if I want to duplicate the database, but
without any data. Do I need to use DMO? I tried this before by DTS, but
everytime when I did this, some definition of fields were changed. For
example, there is a field named "adddate" which default value is
"getDate()". However, after I duplicate it from DTS, the default value
(getdate()) was lost. Maybe I lost something when I did DTS?
Thanks
Q.
"JT" <someone@.microsoft.com> wrote in message
news:%23fQMymiZFHA.3364@.TK2MSFTNGP12.phx.gbl...
> You can restore from a recent backup and specify the new name. Also, you
> can
> detach, copy the mdf file, and then re-attach under a new name. How to do
> this from C# ? You could use data management objects (DMO), but I would
> suggest just writing a stored procedure and calling the SP from C#.
> "Quentin Huo" <q.huo@.manyworlds.com> wrote in message
> news:%23QcXgaiZFHA.3328@.TK2MSFTNGP09.phx.gbl...
>|||The easiest way of doing this is the restore from a recent backup ! You
can restore from a backup using DMO or TSQL with ADO !
Your best bet if you use DTS is to use copy object job. Try the copy
database wizard and reuse the DTS made by it !
Pollus Brodeur

Friday, February 24, 2012

how to do muliplication within UPDATE ?

Hi

I have a product table containing price. I want to update every price in a table by 2.5.

Unfortunately, the price field is actually a varchar(20) and I am not allowed to change it into int because this is a legacy program and the project leads forbid to make change to the database

I did a user-defined scalar function to convert string into int and try the following update statement but all it does is to change 0.0 into 0 .

UPDATE Product_Info SET Price = str(dbo.cval(Price)*2)

Any idea please?

***Before***

9> select price from product_info
10> go
price
--
0.00
0.00
0.00
0.00
1
0.00
0.00
9

**1_pri
**2_pri
**3_pri

*** After ****

1> select price from product_info
2> go
price
--
0
0
0
0
1
0
0
9

0
0
0

The function:

-
User-Defined Scalar Function
-

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: 7 Nov 2006
-- Description: Convert String into Int
-- =============================================
CREATE FUNCTION [dbo].[CVal]
(
-- Add the parameters for the function here
@.input varchar(10)
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
--DECLARE @.val VARCHAR(12)
--DECLARE @.return INT
--SET @.val = @.input

--SET @.return = SELECT CONVERT(INT, LEFT(@.val,PATINDEX('%[^0-9]%',@.val+' ')-1))


RETURN CONVERT(INT, LEFT(@.input,PATINDEX('%[^0-9]%',@.input+' ')-1))

END


incerese by 2.5 or 2.5 times !!!

update Product_Info set price = (cast(price as float) + 2.5)

or if 2.5 times id wat u want

update Product_Info set price = (cast(price as float) * 2.5)

|||

wow....that is amazing

that works perfectly

Thank you