How to combine feet and inches into one combined column - sql-server

Combining last name and first name isn't an issue for me - as shown above. The query spits that out fine. However, in this database schema, inches and feet are separated. When I try to combine them as height_feet + ' ' ' + height_inches, I get an error for obvious reasons. What's the best way to combine feet and inches, so that it reads as 6'7 for instance.
I've tried
,height_feet + ''' + height_inches as 'Height'
I've also tried
,concat(cast(height_feet as char), ''' ', cast(height_inches as char), '"')
Neither have helped me get the desired result. All I want is it to read 5'10 if feet is 5 and inches is 10.
SELECT TOP 10
last_name + ', ' + first_name as 'Employee'
,days as 'Days Employed'
,height_feet
,height_inches
,ROUND(sales, 3) as 'Sales'

The right function to do concatenation in MySQL is CONCAT, therefore use it for all your concatenations.
Your problem is just because you are trying to concatenate a reserved char '. In order to concatenate a single quote you need to escape it so you double it.
SELECT
concat(last_name, ', ', first_name) as 'Employee'
,days as 'Days Employed'
,concat(height_feet, '''',height_inches)
,ROUND(sales, 3) as 'Sales'
from ...
where ...
LIMIT 10 -- "TOP 10" is MSSQL syntax
See an example here: http://sqlfiddle.com/#!9/9eecb/102692
EDIT
I just noticed that you may have tagged your question with MySQL when it should be SQL SERVER. Nevertheless since SQL Server has the same function CONCAT the same query will work, see the same example running here: http://sqlfiddle.com/#!18/9eecb/54309

Related

ABAP SQL preserve OR pad trailing spaces

I am trying to find a way to preserve a space within SQL concatenation.
For context: A table I am selecting from a table with a single concatenated key column. Concatenated keys respect spaces.
Example: BUKRS(4) = 'XYZ ', WERKS(4) = 'ABCD' is represented as key XYZ ABCD.
I am trying to form the same value in SQL, but it seems like ABAP SQL auto-trims all trailing spaces.
Select concat( rpad( tvko~bukrs, 4, (' ') ), t001w~werks ) as key, datab, datbi
from t001w
inner join tvko on tvko~vkorg = t001w~vkorg
left join ztab on ztab~key = concat( rpad( tvko~bukrs, 4, (' ') ), t001w~werks ) "This is why I need the concat
rpad( tvko~bukrs, 4, ' ' ) in this example returns XYZ, instead of XYZ , which leads to concatenated value being XYZABCD, rather than XYZ ABCD.
lpad seems to work just fine (returning XYZ), which leads me to believe I'm doing something wrong.
SQL functions don't accept string literals or variables (which preserve spaces in the same circumstances in ABAP) as they are non-elementary types.
Is there any way to pad/preserve the spaces in ABAP SQL (without pulling data and doing it in application server)?
Update: I solved my problem by splitting key selection from data selection and building the key in ABAP AS. It's a workaround that avoids the problem instead of solving it, so I'll keep the question open in case an actual solution appears.
EDIT: this post doesn't answer the question of inserting a number of characters which vary based on values in some table columns e.g. LENGTH function is forbidden in RPAD( tvko~bukrs, LENGTH( ... ), (' ') ). It's only starting from ABAP 7.55 that you can indicate SQL expressions instead of fixed numbers. You can't do it in ABAP before that. Possible workarounds are to mix ABAP SQL and ABAP (e.g. LIKE 'part1%part2' and then filtering out using ABAP) or to use native SQL directly (ADBC, AMDP, etc.)
Concerning how the trailing spaces are managed in OpenSQL/ABAP SQL, they seem to be ignored, the same way as they are ignored with ABAP fixed-length character variables.
Demonstration: I simplified your example to extract the line Walldorf plant:
These ones don't work (no line returned):
SELECT * FROM t001w
WHERE concat( 'Walldorf ' , 'plant' ) = t001w~name1
INTO TABLE #DATA(itab_1).
SELECT * FROM t001w
WHERE concat( rpad( 'Walldorf', 1, ' ' ), 'plant' ) = t001w~name1
INTO TABLE #DATA(itab_2).
These 2 ones work, one with leading space(s), one using concat_with_space:
SELECT * FROM t001w
WHERE concat( 'Walldorf', ' plant' ) = t001w~name1
INTO TABLE #DATA(itab_3).
SELECT * FROM t001w
WHERE concat_with_space( 'Walldorf', 'plant', 1 ) = t001w~name1
INTO TABLE #DATA(itab_4).
General information: ABAP documentation - SQL string functions
EDIT: working example added, using leading space(s).

T-SQL - Show newline constant in results

I've been having trouble with a query on a database I'm working with. The database would for instance return 18 results, and the LINQ query in the parent .NET project, would return 27 results. I've found this is down to newlines, as the database was created out of an Excel document.
Is there a way in SQL Server Manangement Studio to show these new line characters in the cells of the results of a query. Just something like printing "\n" on the end of all cells that have this new line character?
I've tried the following query WHERE statements but with no joy:-
AND (FieldName like '%Text String%' + CHAR(10))
AND (FieldName like '%Text String%' + CHAR(13))
AND (FieldName like CHAR(10) + '%Text String%' + CHAR(10))
AND (FieldName like CHAR(13) + '%Text String%' + CHAR(13))
AND (FieldName like CHAR(10) + '%Text String%')
AND (FieldName like CHAR(13) + '%Text String%')
as I read How can I check if an SQL result contains a newline character? with these CHAR(10) and CHAR(13) as ways of testing if there is a new line in the field.
Is there a way to print these new lines in the fields of the results of the query?
UPDATE
As an example:-
The following returns 0 results:-
AND (DrillType = 'Plain Bore')
but the following returns 5 results:-
AND (L3Bore = 'Plain Bore
')
Note, the closing quote and bracket on a new line. Does this make any sense as to why this would be happening?
Thanks in advance!

SQL Server : Like Syntax and Add operator Error

I am using this syntax and it should work well if only it doesn't get an error.
SELECT
e.user_key,
e.char_key,
CONVERT(VARCHAR,substring(e.char_data, 9, 16)) AS name,
p.CHAR_DATA
FROM
CHAR_DATA0 AS e
INNER JOIN
CHAR_DATA1 AS p ON e.CHAR_KEY = p.CHAR_KEY
WHERE
p.CHAR_DATA LIKE '%'+CAST(cast(reverse(CONVERT(BINARY, 9998)) as BINARY(2)) AS BINARY(2))+'%'
ORDER BY
char_key
I need help. How to change this code for it to work?
CHAR_DATA column is of type BINARY(2000)
I have data like so: (got it using select)
0x04005C03020F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBA3B8C08000000BB3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBC3B8C08000000BD3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBE3B8C08000000BF3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFC03B8C08000000C13B8C08000000B5388B08020000B5388B08020000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBA3B8C08000000BB3B8C08000000B5388B08020000B5388B08020000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBC3B8C08000000BD3B8C080000000E2789080100000E278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBE3B8C08000000BF3B8C080000000E2789080100000E278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFC03B8C08000000C13BFFFF000000FFFFFFFFFFFFFFFFFFFFFFFFFFFF0F27FFFF0100000F27FFFF0100000E2789080000000E278908000000FFFFFFFFFFFFFF7E107F08000000EC168408000000B0177D080000000F27FFFF0100000F27FFFF0100000E2789080000000E27890800000052167C081E000052167C081E0000E2167D08000000271F2005000000FFFFFFFFFFFFFFFFFFFFFFFFFFFF0E2787080100000E27870801000052167C081E000052167C081E0000FFFFFFFFFFFFFF13127F08000000C813FFFF1E0000C813FFFF1E00000E2787080100000E27870801000028127A08000000BF3B2D05000000FFFFFFFFFFFFFF13127F08000000C813FFFF1E0000C813FFFF1E0000FFFFFFFFFFFFFFFFFFFFFFFFFFFF28127A08000000FFFFFFFFFFFFFF1B1A80081A00001B1A80081A0000E915FFFF1B0000E915FFFF1B000023067A0800000024147B0800000020120D05000000084792080100001B1A80081A00001B1A80081A0000E915FFFF1B0000E915FFFF1B000025067A0800000024147B0800000020120D05000000E21610050000000000009D000000FFFF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
I need to search somewhere in those lines if it contains the entry I'm trying to find.
What I'm trying to find is always 2 bytes. So I'm looking for 0x0E27 in the syntax above.
This is a working syntax but its not what I want to do:
SELECT
CONVERT(INT,cast(reverse(substring(char_data, 37, 2)) as BINARY(2))) AS helm
FROM CHAR_DATA0
WHERE CHAR_KEY = 5
Another this is if I use nvarchar(MAX) it gives results that doesn't even contain the data I'm trying to find.
where Convert(varChar(4000), CHAR_DATA,2 ) like '%' + Convert(varChar(4), CONVERT(BINARY(2), 9998),2 )+'%'
I tried just selecting that like clause, mainly because I don't need your schema (tables) to do so, and found a bug (I think) already:
SELECT '%' + CAST( CAST(REVERSE(CONVERT(BINARY, 9998)) as BINARY(2)) AS BINARY(2) ) + '%'
produces:
Msg 402, Level 16, State 1, Line 1
The data types varchar and binary are incompatible in the add operator.
... where as (changing the overal result type to varchar):
SELECT '%' + CAST( CAST(REVERSE(CONVERT(BINARY, 9998)) as BINARY(2)) AS VARCHAR(2) ) + '%'
produces (we'll need some luck with the encoding here):
%'% -- that is: percent, apostrophe, beam (the musical note), percent
Maybe that's the whole problem (though I doubt it)? Try it and let-is know how you go. I doubt it because I guess that if it was the whole problem then you would have worked it out for yourself from the error message (that I presume you would have gotten from SQL-Server).
Cheers. Keith.

SQL Server Express 2008 R2 - Square brackets in text field not returning results

I've created a simple stored procedure with some default values to enable customised searching of my "MachineModel" database.
As part of the WHERE clause I have something like this:
Model.[ModelName] LIKE '%' + ISNULL(#ModelName, Model.[ModelName]) + '%'
which should allow for partial matches. This works for the majority of models, however if no value is supplied for #ModelName (and therefore the ISNULL resolves to Model.[ModelName]) and the underlying field data contains square brackets, such as "Lenny's Test Model [2010]", then those records records aren't returned by the SProc.
This isn't a huge problem as only 4 models (of about 120,000) actually have square brackets in their names so I could easily change them, however I'm curious to know what's going on and what's the best way to solve this.
Cheers,
Lenny.
You need to replace the leading bracket by [[]
There are 3 characters with meaning in LIKE: % _ [. You don't need to escape the trailing bracket.
For completeness:
Model.[ModelName] LIKE '%' + ISNULL(
--don't do it the other way around!
REPLACE(REPLACE(REPLACE(#ModelName, '[', '[[]), '%', '[%]), '_', '[_]),
Model.[ModelName]) + '%'
Why are you comparing the model with itself in case of null search parameter? This is expensive.
It is also not great practice to mix ands and ors with SQL Server as its expensive, but could be done:
Where (
#ModelName is null OR
(ModelName is not null AND ModelName LIKE '%' + #ModelName + '%')
)
if performance could be a prob, then introducing an if can help, but you do end up duplicating your select
if #ModelName is null
select * from ...
else
select ...
where ModelName LIKE '%' + #ModelName + '%'
You could, but it will upset your dba, build the sql up in code and use sqlparameters and only add the where when this var is not null

SQL Server : LENGTH() inside of REPLICATE()

I have a SQL Server table with the columns Lvl and Title. I need to insert a "-" in front of the title for every character in the Lvl field.
As an example: If Lvl = 111 the title should become --- My Title.
I can only edit the following SQL string. There is no possibility to create other functions or likewise.
SELECT REPLICATE('_', { fn LENGTH(Lvl) }) + ' ' + Title AS Title
FROM Documents
My problem is, that the LENGTH() function doesn't work inside the REPLICATE() function. Does anybody know why or how to solve this problem?
Thank you.
Try this:
SELECT REPLACE(Lvl, '1', '-') + ' ' + Title as Title
FROM Documents
Simply take the Lvl column, and replace all instances of 1 with whatever character you want, then concatenate Title to the end of the result.
Try this. It works fine for me -
select REPLICATE('-',LEN(Lvl)) + ' ' + Title as title from documents

Resources