Solved

Need some help with CREATE A VIEW with CAST

Posted on 2011-03-09
10
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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