Solved

Need some help with CREATE A VIEW with CAST

Posted on 2011-03-09
10
259 Views
Last Modified: 2012-05-11
Hello
I would like to create a view =ViewLieferscheine1.
In the SQL Table the A.VersandPostleitzahl is a int.

I would like to CAST the int to a nvarchar.
CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl......that is wrong and will not do it
 
How can I do this. Please help.
500 points with a solution.
Thank you.
Best regards,
Thomas

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
	  A.LieferscheinNr, A.VersandName1, A.VersandName2,\
	  A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl,\
	  A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
	  A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
	  A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
	  FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
	  ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<%d\
	  GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
	  A.VersandStrassePostfach, A.VersandPostleitzahl,\
   	  A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
	  A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);

Open in new window

0
Comment
Question by:tsp2002
  • 7
  • 3
10 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
Comment Utility
Hi,


CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl

Remove A.

CAST(A.VersandPostleitzahl AS CHAR)AS VersandPostleitzahl



- Bhavesh
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

You no need to give Table Alias while giving column alias.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi again,

Secondly your where condition seems not complete

WHERE A.LieferscheinNr<%d

This is not valid syntax.
0
 

Author Comment

by:tsp2002
Comment Utility
Hello,
okay that works....thank you.

 A.LieferscheinNr<%d
how should I change this...also with a cast ?
The view should only have LieferscheinNr with a value <2000000
LieferscheinNr is a nvarchar in my SQL table.
Please let me know.
Thank you.


0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

You can directly pass the value.
you no need to cast

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
          A.LieferscheinNr, A.VersandName1, A.VersandName2,\
          A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
          A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
          A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
          FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
          ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<2000000\
          GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
          A.VersandStrassePostfach, A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);




secondly...try to use VarChar instead of Char with specifying size.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
In above query,

I used VarChar(10) as example...

Please use actual size....

Your final view will be like .....
CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
FROM 
		Lieferscheine AS A 
inner join Lieferscheinpositionen AS B ON A.LieferscheinNr=B.LieferscheinNr 

WHERE A.LieferscheinNr<2000000

GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,
          A.VersandStrassePostfach, A.VersandPostleitzahl,
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv

Open in new window

0
 

Author Closing Comment

by:tsp2002
Comment Utility
okay...thanks for your help.
That was fast and easy.
Have a great day.
Best regards,
Thomas
0
 

Author Comment

by:tsp2002
Comment Utility

CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
 
you forgot to remove the A.
 AS VARCHAR(10))AS A.VersandPostleitzahl




CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht

 
That was my error in the first place...thank you best regards to India from Germany

Open in new window

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

You used varchar(10).

Please recheck the size with your table.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
happy to assist you.
You to have a great time ahead.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now