| Author |
Topic |
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 15:37:09
|
Hi,I want to run the below query in SQL Server, could anyone help me in thisQL> !cat q.sqlWITH unix_perms AS( SELECT 'lrwxrwxrwx' AS perm FROM DUAL UNION ALL SELECT 'drwxr-xr-x' AS perm FROM DUAL UNION ALL SELECT '-rw-r--r--' AS perm FROM DUAL UNION ALL SELECT '-rw-------' AS perm FROM DUAL)/* End Sample Data */select perm ,decode(translate(substr(perm,2,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7') ||decode(translate(substr(perm,5,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7') ||decode(translate(substr(perm,8,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7') dec ,case substr(perm,1,1) when 'l' then 'sym' else to_char(777 - to_number(decode(translate(substr(perm,2,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7') ||decode(translate(substr(perm,5,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7') ||decode(translate(substr(perm,8,3),'rwx-','1110'),'000','0','001','1','010','2','011','3','100','4','101','5','110','6','111','7')),'fm000') end umskfrom unix_permsPERM DEC USK---------- --- --------lrwxrwxrwx 777 symdrwxr-xr-x 755 022-rw-r--r-- 644 133-rw------- 600 177 -Neil |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-09 : 15:40:47
|
| the posted query doesnt correspond to t-sql syntax. where are you trying to run this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 15:57:41
|
| Hi Visakh this is Oracle query, I want to run a similar query in SQL Server to get the same output.-Neil |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-09 : 16:05:49
|
| ok you need to replace the below|| with + for concatenationdecode with CASE...WHEN...THEN...ELSEtranslate with REPLACE()substr with SUBSTRING()to_char to CONVERT(char(xx),...)to_number to CONVERT(int,...)look for syntaxes of corresponding functions in msdn/books online------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 16:28:44
|
Thanks Visakh, I'll try to change this step oneWITH unix_perms AS( SELECT 'lrwxrwxrwx' AS perm UNION ALL SELECT 'drwxr-xr-x' AS perm UNION ALL SELECT '-rw-r--r--' AS perm UNION ALL SELECT '-rw-------' AS perm ) select * from unix_perms -Neil |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-09 : 16:47:51
|
quote: Originally posted by aakcse Thanks Visakh, I'll try to change this step oneWITH unix_perms AS( SELECT 'lrwxrwxrwx' AS perm UNION ALL SELECT 'drwxr-xr-x' AS perm UNION ALL SELECT '-rw-r--r--' AS perm UNION ALL SELECT '-rw-------' AS perm ) select * from unix_perms -Neil
this will work in t-sql as well------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 16:49:20
|
Changed the code but not getting the desired result as Ora..WITH unix_perms AS( SELECT 'lrwxrwxrwx' AS perm UNION ALL SELECT 'drwxr-xr-x' AS perm UNION ALL SELECT '-rw-r--r--' AS perm UNION ALL SELECT '-rw-------' AS perm ) select perm ,CASE replace(substring(perm,2,3),'rwx-','1110') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(substring(perm,5,3),'rwx-','1110') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN'7' ELSE '' END +CASE replace(substring(perm,8,3),'rwx-','1110') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN'7' ELSE '' END ,case substring(perm,1,1) when 'l' then 'symbolic' else cast(777 - cast(CASE replace(substring(perm,2,3),'rwx-','1110') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN'7' ELSE '' END +CASE replace(substring(perm,5,3),'rwx-','1110') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN'7' ELSE '' END +CASE replace(substring(perm,8,3),'rwx-','1110')WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN'7' ELSE '' END as INT) as varchar) end umskfrom unix_permsperm (No column name) umsklrwxrwxrwx symbolicdrwxr-xr-x 777-rw-r--r-- 777-rw------- 777 -Neil |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 16:50:41
|
| looks like 'fm000' is some formatting in Ora and few more changes needed to get the o/p as ora-Neil |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 19:18:19
|
| Hi Visakh,Is there anything we can do with the above query..-Neil |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-09 : 20:07:55
|
Oh Visakh, here Replace will not work need to do a rework I'll post the query once completed Sir.. thanksselect replace(substring('rwx',1,3),'rwx-','1110')--rwx-Neil |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-10 : 10:01:13
|
quote: Originally posted by aakcse Oh Visakh, here Replace will not work need to do a rework I'll post the query once completed Sir.. thanksselect replace(substring('rwx',1,3),'rwx-','1110')--rwx-Neil
how can you find pattern which is longer than actual string? so above replace doesnt make any sense------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-10 : 14:05:00
|
Hi Visakh, I got it, the below query worked for meWITH unix_perms AS( SELECT 'lrwxrwxrwx' AS perm UNION ALL SELECT 'drwxr-xr-x' AS perm UNION ALL SELECT '-rw-r--r--' AS perm UNION ALL SELECT '-rw-------' AS perm ) select perm ,CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101'THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END ,CASE substring(perm,1,1) when 'l' then 'symbolic' else CASE LEN(cast(777 - cast(CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END as INT) as varchar) ) WHEN 0 THEN '000'+ cast(777 - cast(CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END as INT) as varchar) WHEN 1 THEN '00' + cast(777 - cast(CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END as INT) as varchar) WHEN 2 THEN '0' + cast(777 - cast(CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END as INT) as varchar) ELSE cast(777 - cast(CASE replace(replace(replace(replace(substring(perm,2,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,5,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END +CASE replace(replace(replace(replace(substring(perm,8,3),'r','1'),'w','1'),'x','1'),'-','0') WHEN '000' THEN '0' WHEN '001'THEN '1'WHEN '010' THEN '2' WHEN '011' THEN '3' WHEN '100' THEN '4' WHEN '101' THEN '5' WHEN '110' THEN '6' WHEN '111' THEN '7' ELSE '' END as INT) as varchar) END END as umsk from unix_perms -Neil |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-10 : 15:19:26
|
| cool...great stuff!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-08-16 : 10:16:22
|
| Also prefix ; with WITHMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-16 : 14:39:07
|
quote: Originally posted by madhivanan Also prefix ; with WITHMadhivananFailing to plan is Planning to fail
Only required if its within a batch with preceding statements------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-08-17 : 09:28:26
|
quote: Originally posted by visakh16
quote: Originally posted by madhivanan Also prefix ; with WITHMadhivananFailing to plan is Planning to fail
Only required if its within a batch with preceding statements------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes but for the sake of safety always use it MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-17 : 10:06:44
|
quote: Originally posted by madhivanan
quote: Originally posted by visakh16
quote: Originally posted by madhivanan Also prefix ; with WITHMadhivananFailing to plan is Planning to fail
Only required if its within a batch with preceding statements------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes but for the sake of safety always use it MadhivananFailing to plan is Planning to fail
yep... thats anyways a good practise ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|