Solved

Need some help with CREATE A VIEW with CAST

Posted on 2011-03-09
10
260 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
ID: 35082403
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
ID: 35082412
Hi,

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

Expert Comment

by:Bhavesh Shah
ID: 35082424
Hi again,

Secondly your where condition seems not complete

WHERE A.LieferscheinNr<%d

This is not valid syntax.
0
 

Author Comment

by:tsp2002
ID: 35082788
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
ID: 35082870
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082900
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
ID: 35082950
okay...thanks for your help.
That was fast and easy.
Have a great day.
Best regards,
Thomas
0
 

Author Comment

by:tsp2002
ID: 35083027

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
ID: 35083049
Hi,

You used varchar(10).

Please recheck the size with your table.
0
 
LVL 19

Expert Comment

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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

23 Experts available now in Live!

Get 1:1 Help Now