trying to convert DB2 query ORDER BY condition into SQL Server
DB2 Query
ORDER BY
CASE WHEN LEN(RTRIM(LTRIM(CorpName))) > 1 THEN CorpVal Else '999' END,
CASE WHEN SUBSTRING(FName,1,1) != '*' THEN FName Else '999' END
SQL Query
ORDER BY
CASE WHEN CorpName like '[a-z]%' THEN 0 ELSE 1 END,
CASE WHEN FName like '[a-z]%' THEN 0 ELSE 1 END
I have the data something like
ABC,
24KS,
ABE,
AJX,
-Extra,
ABF,
1X1
I need the output like below
ABC,
ABE,
ABF,
AJX,
24KS,
1X1,
-Extra
this does not works for me, need some more suggestion.
Ordering is determined by collations in SQL Server and DB2. It seems your iSeries DB2 is configured with an EBCDIC collation so you could add an explict COLLATE clause to the ORDER BY expression to coerce EBCDIC ordering rules for SQL Server since your SQL Server collation is apparently different.
Below is an example of your original DB2 query with the clause added for the SQL Server:
ORDER BY
CASE WHEN LEN(RTRIM(LTRIM(CorpName))) > 1 THEN CorpVal Else '999' END COLLATE SQL_EBCDIC037_CP1_CS_AS,
CASE WHEN SUBSTRING(FName,1,1) != '*' THEN FName Else '999' END COLLATE SQL_EBCDIC037_CP1_CS_AS
Related
Are there any features within Microsoft SQL Server TSQL that could shorten this CASE WHEN statement?
CASE
WHEN some_column IS NULL
THEN 0
ELSE 1
END
For SQL Server 2012 and later you can use IIF() statement.
SELECT IIF(some_column IS NULL , 0 , 1)
You could use what SQL Server documentation calls the "simple" case expression, instead of the "search" case expression that the syntax in the question uses.
case some_column when null then 0 else 1 end
Not a large difference, but it is shorter.
I have copied the results from SQL Server except the last column (expected_rn_af) which was added later.
How can I convert column rn_af to expected_rn_af using update statement in SQL Server?
Add 64+rn_af within char()
Update YourTable
Set Expected_Rn_af = char(64+rn_af)
Looks like you want a case statement...
...
case
when rn_af = 1 then 'A'
when rn_af = 2 then 'B'
...
end as expected_rn_af
I have the following query that I wanted to convert to in SQL Server but not sure how I should go about this. Should I use a CASE statement because I don't see any true/false conditions in this statement. How should I go about this? Thanks!
ORACLE Query:
IIF(CD_ID = '999999',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || '999.99',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || TO_CHAR(CD_ID))
From SQL SERVER 2012+ we can use CONCAT function.
SELECT IIF(CD_ID = '999999',CONCAT(CD_SEQ_NO, '999.99'),CONCAT(CL_ID,CD_SEQ_NO,CD_ID))
FROM YOURTABLE
Concat function not only concatenate the strings but also does implicit conversion when required
Anything less then SQL SERVER 2012 use CASE statements with explicit conversions to varchar
SELECT CASE
WHEN cd_id = '999999' THEN Cast(cd_seq_no AS VARCHAR(50)) + '999.99'
ELSE Cast(cl_id AS VARCHAR(50))+Cast(cd_seq_no AS VARCHAR(50))+Cast(cd_id AS VARCHAR(50))
END
FROM yourtable
I'm having trouble trying to clean a database because SQL Server doesn't differentiate '2¹59' from '2159', but when when try to convert into INT it obviously returns an error.
In this case I need to replace by NULL, every non numerical data.
Can someone help please? (I'm using Sql Server 2008)
From SQL SERVER 2012 there is a new function which have been added called TRY_PARSE,
If you use it then it will automatically make non int to null.
select TRY_PARSE('2¹59' as int)
Output of above query will be null.
You can use a different collation to change the way the strings are compared:
select
case when N'2¹59' = N'2159' collate Latin1_General_BIN then 1 else 0 end
This will select 0 as you'd expect.
More importantly, since MS SQL understands unicode properly, you can do this:
select cast(N'2¹59' as varchar)
which will give you '2159' - properly replacing the "broken" digits.
If you have no other option, you could also build a helper table to handle indexing the string (just a single column with numbers 1..1000 for example), and do something like this:
exists
(
select 1 from [Numbers]
where
[Numbers].[Index] < len([Value]) + 1
and
unicode(substring([Value], [Numbers].[Index], 1)) > 127
)
Needless to say, this is going to be rather slow. For simple integers, though, this can work as a decent validation - simply use (unicode(substring([Value], [Numbers].[Index], 1)) not between 48 and 57) and ([Numbers].[Index] <> 0 or substring([Value], 1, 1) <> '-')) for example.
I am completely confused about the default string comparison method used in Microsoft SQL Server. Up till now I had been using UPPER() and LOWER() functions for performing any string comparison on Microsoft SQL Server.
However got to know that by default Microsoft SQL Server is case insensitive and we need to change the collation while installing Microsoft SQL Server to make it case sensitive. However if this is the case then what is the use of UPPER and LOWER() functions.
if you like to compare case sensitive string this might be the syntax you looking for
IF #STR1 COLLATE Latin1_General_CS_AS <> #STR2 COLLATE Latin1_General_CS_AS
PRINT 'NOT MATCH'
As you have discovered, upper and lower are only of use in comparisons when you have a case-sensitive collation applied, but that doesn't make them useless.
For example, Upper and Lower can be used for formatting results.
select upper(LicencePlate) from cars
You can apply collations without reinstalling, by applying to a column in the table design, or to specific comparisons ie:
if 'a' = 'A' collate latin1_general_cs_as
select '1'
else
select '2'
if 'a' = 'A' collate latin1_general_ci_as
select '3'
else
select '4'
See http://technet.microsoft.com/en-us/library/aa258272(v=sql.80).aspx